- Forum-Beiträge: 12
22.12.2011, 13:17:08 via Website
22.12.2011 13:17:08 via Website
ich bin ja hier immer fleissig am mitlesen - leider selten am mitposten...
Hab schon einiges zu meinem Problem im Internet recherchiert, konnte aber keine Loesung finden - es ist mir ein Raetsel
Problem:
Ich habe eine SimpleArrayAdapter Klasse geschrieben, die mir eine ListView mit ListItems im CustomLayout ausgeben soll. Das Ganze hat auch wunderbar funktioniert, bis sich das System dann doch entschieden hat eine NullPointerException rauszugeben ;)
Die ListView ist in der Activity vorhanden und auch initialisiert:
2
3onCreate() {
4...
5initializeViews();
6...
7}
8
9initializeViews() {
10...
11listView = (ListView) findViewById(android.R.layout.list);
12...
13}
Die ListView ist im main.xml folgendermassen implementiert:
2 android:id="@id/android:list"
3 android:layout_width="fill_parent"
4 android:layout_height="wrap_content"
5 android:drawSelectorOnTop="false"
6 />
... das ist aber auch nicht das Problem ansich, das Problem entsteht beim initialisieren der Views im SimpleArrayAdapter Object. Die list_item.xml sieht folgendermassen aus:
2<LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 android:orientation="horizontal"
5 android:layout_width="fill_parent"
6 android:layout_height="wrap_content"
7 android:background="@color/activity_background"
8 >
9 <ImageView
10 android:layout_width="wrap_content"
11 android:src="@drawable/blue_list_icon"
12 android:layout_height="fill_parent"
13 android:layout_marginRight="5dp">
14 </ImageView>
15
16 <TextView
17 android:id="@+id/txt_liv_item_myitem"
18 android:textAppearance="?android:attr/textAppearanceMedium"
19 android:layout_width="wrap_content"
20 android:text="TextView"
21 android:layout_height="wrap_content"
22 android:textColor="@color/txt_sum"
23 android:layout_marginRight="15dp"
24 />
25
26 <TextView
27 android:id="@+id/txt_liv_item_mytime"
28 android:textAppearance="?android:attr/textAppearanceMedium"
29 android:layout_width="wrap_content"
30 android:text="TextView"
31 android:layout_height="wrap_content"
32 android:textColor="@color/txt_sum"
33 android:layout_marginRight="15dp"
34 />
35
36 <TextView
37 android:id="@+id/txt_liv_item_filler_until"
38 android:textAppearance="?android:attr/textAppearanceMedium"
39 android:layout_width="wrap_content"
40 android:text="@string/txt_liv_item_filler_until"
41 android:layout_height="wrap_content"
42 android:textColor="@color/txt_sum"
43 android:layout_marginRight="15dp"
44 />
45
46
47 <TextView
48 android:id="@+id/txt_liv_item_time"
49 android:textAppearance="?android:attr/textAppearanceMedium"
50 android:layout_width="wrap_content"
51 android:text="TextView"
52 android:layout_height="wrap_content"
53 android:textColor="@color/txt_sum"
54 android:layout_marginRight="15dp"
55 />
56</LinearLayout>
Im SimpleArrayAdapter kommt die Fehlermeldung bei jedem Versuch ein TextView in der getView() Methode zuzuweisen... aber seht selbst
2 public View getView(int position, View convertView, ViewGroup parent) {
3 Log.v(TAG, "called [getView(int,View,ViewGroup)], position=" + position);
4 Log.v(TAG, "Value at position " + position + " info:\n" + values.get(position).toString());
5
6 LayoutInflater inflater = (LayoutInflater)context
7 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
8
9 View listItem = inflater.inflate(R.layout.list_item, parent, false);
10
11 if(listItem == null) {
12 Log.e(TAG, "listeItem == null!!!"); // wird nie ausgegeben und ist somit !=NULL
13 // deswegen gehe ich davon aus, dass hier noch alles ok ist
14 }
15
16 TextView txtMyVal = (TextView) listItem.findViewById(R.id.txt_liv_item_myvalue);
17 Log.d(TAG, "Calling value at position: " + position);
18 if(tickets.get(position) == null) {
19 Log.d(TAG, "value does not exist!");// wird nie ausgegeben, daher value !=NULL
20 // hier kann der fehler also auch nicht liegen
21 }
22 if(txtLpn == null) {
23 Log.w(TAG, "could not find TextView txtMyVal "); // das wird aber ausgegeben, hier haperts also schon!
24 }
25 Log.d(TAG, "My Value: " + values.get(position).getInfo());
26
27 try {
28 txtMyVal.setText(values.get(position).getInfo());
29 } catch(Exception e) {
30 Log.e(TAG, "Got Exception while setting text on txtMyVal: " + e.toString(), e); // diese Exception wird ausgegeben!! WARUM??
31 }
32
33 return listItem;
34 }
Meine derzeitigen Erkenntnisse habe ich oben im Code kommentiert ;)
Vielen Dank im Vorraus,
cheers
Barrachiel
— geändert am 22.12.2011, 13:18:23
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.