NullPointerException bei TextViewArray

  • Antworten:3
  • Bentwortet
Kirs Ten
  • Forum-Beiträge: 3

15.10.2012, 12:58:23 via Website

Hallo liebe Community,

da ich mit meinem Latein am Ende bin, wende ich mich nun hoffnungsvoll an Euch.
Folgendes Problem: Ich habe ein TextView-Array, das ich mit TextViews füllen will, die ihre Zuweisungen über eine For-Schleife bekommen.
Dazu lese ich die IDs der jeweiligen TextViews aus, um sie dann dem Array via findViewById hinzuzufügen. Leider schmeißt er mir eine Fehlermeldung, sowie er dem Array ein TextView hinzufügen soll...

Die XML:
1<TextView
2android:id="@+id/bestandteil_1"
3android:layout_width="400dp"
4android:layout_height="40dp"
5android:layout_marginLeft="5dp"
6android:text="@string/dian_motor_motor_antrieb"
7android:textColor="#ffffff"
8android:textSize="20dp" />
9
10<TextView
11android:id="@+id/bestandteil_2"
12android:layout_width="400dp"
13android:layout_height="40dp"
14android:layout_marginLeft="5dp"
15android:text="@string/dian_motor_motor_antrieb"
16android:textColor="#ffffff"
17android:textSize="20dp" />
18
19<TextView
20android:id="@+id/bestandteil_3"
21android:layout_width="400dp"
22android:layout_height="40dp"
23android:layout_marginLeft="5dp"
24android:text="@string/dian_motor_motor_antrieb"
25android:textColor="#ffffff"
26android:textSize="20dp" />

Die Activity:
[code]private TextView[] bestandteil;
private int maxAnz = 3;

public void speichern() {
for (int i = 1; i <= maxAnz; i++) {
String bestandteilID = "bestandteil_" + i;
int resID = getResources().getIdentifier(bestandteilID, "id", getPackageName());
bestandteil[i] = (TextView) findViewById(resID);
}
}
[/code]

Die Fehlermeldung:
110-15 12:44:33.410: E/AndroidRuntime(11781): FATAL EXCEPTION: main
210-15 12:44:33.410: E/AndroidRuntime(11781): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.szenaris.activity/com.szenaris.activity.DIAN_Modell_Motorraum}: java.lang.NullPointerException
310-15 12:44:33.410: E/AndroidRuntime(11781): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
410-15 12:44:33.410: E/AndroidRuntime(11781): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
510-15 12:44:33.410: E/AndroidRuntime(11781): at android.app.ActivityThread.access$600(ActivityThread.java:123)
610-15 12:44:33.410: E/AndroidRuntime(11781): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
710-15 12:44:33.410: E/AndroidRuntime(11781): at android.os.Handler.dispatchMessage(Handler.java:99)
810-15 12:44:33.410: E/AndroidRuntime(11781): at android.os.Looper.loop(Looper.java:137)
910-15 12:44:33.410: E/AndroidRuntime(11781): at android.app.ActivityThread.main(ActivityThread.java:4424)
1010-15 12:44:33.410: E/AndroidRuntime(11781): at java.lang.reflect.Method.invokeNative(Native Method)
1110-15 12:44:33.410: E/AndroidRuntime(11781): at java.lang.reflect.Method.invoke(Method.java:511)
1210-15 12:44:33.410: E/AndroidRuntime(11781): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
1310-15 12:44:33.410: E/AndroidRuntime(11781): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
1410-15 12:44:33.410: E/AndroidRuntime(11781): at dalvik.system.NativeStart.main(Native Method)
1510-15 12:44:33.410: E/AndroidRuntime(11781): Caused by: java.lang.NullPointerException
1610-15 12:44:33.410: E/AndroidRuntime(11781): at com.szenaris.activity.DIAN_Modell_Motorraum.speichern(DIAN_Modell_Motorraum.java:89)
1710-15 12:44:33.410: E/AndroidRuntime(11781): at com.szenaris.activity.DIAN_Modell_Motorraum.onCreate(DIAN_Modell_Motorraum.java:80)
1810-15 12:44:33.410: E/AndroidRuntime(11781): at android.app.Activity.performCreate(Activity.java:4465)
1910-15 12:44:33.410: E/AndroidRuntime(11781): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
2010-15 12:44:33.410: E/AndroidRuntime(11781): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
2110-15 12:44:33.410: E/AndroidRuntime(11781): ... 11 more

Dabei ist Zeile 89: bestandteil[i] = (TextView) findViewById(resID);
Und Zeile 80: speichern() im OnCreate();

Ich denke, es ist nur eine Kleinigkeit... Aber was habe ich übersehen? Bitte öffnet mir die Augen!

Vielen Dank im Voraus.

Grüße, Kirsten

Antworten
Kirs Ten
  • Forum-Beiträge: 3

15.10.2012, 13:38:30 via Website

Oh man :*)... dummer Fehler! Vielen Dank für den Hinweis :)

1private TextView[] bestandteil = new TextView[3];

Antworten