ListView zeigt nix an

S

SirMArtin

Gast
Hallo zusammen,

ich habe ein wenig Probleme mit der ListView. Nach einigem Lesen in der Doku bin ich zu folgendem Code gekommen:

ResultViewer.java
Code:
public class ResultViewer extends ListActivity {
  private static final String LONG_DATE_FORMAT = "dd MMM yyyy hh:mm";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result);
    String pkgNo = this.getIntent().getStringExtra("package_id");
    putResult(pkgNo);
  }

  private void putResult(String pkgnr) {

// getting the result from dummy data
    History res = XMLParser.parse(null);

    if (res == null) {
      Toast.makeText(getBaseContext(), "Es konnte kein Ergebnis abgerufen werden.", Toast.LENGTH_LONG).show();
    } else {
      // matching the result to the UI

      List<Map<String, String>> content = new ArrayList<Map<String, String>>();
      for (Event event : res.getEvents()) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("date", event.getDisplayDate());
        map.put("status", event.getDisplayStatus());
        content.add(map);
      }

      // Create an array to specify the fields we want to display in the list
      // (only TITLE)
      String[] from = new String[] { "date", "status" };

      // and an array of the fields we want to bind those fields to (in this
      // case just text1)
      int[] to = new int[] { R.id.result_item_date, R.id.result_item_status };

      SimpleAdapter adapter = new SimpleAdapter(this, content, R.layout.result_list_item, from, to);
      setListAdapter(adapter);
    }
  }
}

result.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@+id/lbl_result_title" style="@style/Caption"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:text="@string/lbl_result_title" android:layout_marginLeft="10px"
        android:layout_marginTop="0px" />
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:stretchColumns="1">

        <TableRow>
            <TextView android:id="@+id/lbl_tracking_id" style="@style/Label"
                android:layout_column="1" android:text="@string/lbl_tracking_id"
                android:layout_marginLeft="2px" android:layout_marginTop="2px" />
            <TextView android:id="@+id/tracking_id" android:gravity="right"
                android:text="@string/result_status_text_default"
                android:layout_marginRight="2px" android:layout_marginTop="2px" />
        </TableRow>

        <TableRow>
            <TextView android:id="@+id/lbl_result_date" style="@style/Label"
                android:layout_column="1" android:text="@string/lbl_result_date"
                android:layout_marginLeft="2px" android:layout_marginTop="2px" />
            <TextView android:id="@+id/result_date" android:gravity="right"
                android:text="@string/result_date_default"
                android:layout_marginRight="2px" android:layout_marginTop="2px" />
        </TableRow>
    </TableLayout>

    <ListView android:id="@+id/list"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_marginTop="0px"
        android:scrollbars="vertical">
        
    </ListView>
    
</LinearLayout>

Führe ich die App nun aus, bekomme ich immer die Fehlermeldung:
Code:
09-07 09:33:26.877: WARN/dalvikvm(1477): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
09-07 09:33:26.877: ERROR/AndroidRuntime(1477): Uncaught handler: thread main exiting due to uncaught exception
09-07 09:33:26.897: ERROR/AndroidRuntime(1477): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.keineantwort.android.packagetracer/de.keineantwort.android.packagetracer.ResultViewer}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at android.os.Looper.loop(Looper.java:123)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at android.app.ActivityThread.main(ActivityThread.java:3948)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at java.lang.reflect.Method.invokeNative(Native Method)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at java.lang.reflect.Method.invoke(Method.java:521)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at dalvik.system.NativeStart.main(Native Method)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at android.app.ListActivity.onContentChanged(ListActivity.java:236)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:312)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at android.app.Activity.setContentView(Activity.java:1626)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at de.keineantwort.android.packagetracer.ResultViewer.onCreate(ResultViewer.java:27)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
09-07 09:33:26.897: ERROR/AndroidRuntime(1477):     ... 11 more

Das kann ich mir nicht erklären, da ich ja ne ListView mit der ID "list" habe. Wobei ich das auch Käse finde. Ich hätte ja gern ggf. mehrere ListActivities in meiner App und AFAIK ist die ID in der XML unique innerhalb der App.

Hat einer ne Idee, was ich falsch mache?

SirMArtin
 
Im Layout Ordner muss in der XML Datei, welche das layout beschreibt die ListView folgendermaßen heissen: "@+id/android:list" (Siehe Fehlerbeschreibung 3 Zeile)

z.B.

<ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_y="35px" android:layout_height="330px"></ListView>
 
Hm, das hab ich aus der Fehlermeldung nicht gelesen. Ich hätte eher das android:id="" vermutet. :(

Danke für den Hinweis!

Jetzt ist die Exception weg, aber es wird immer noch nix angezeigt. :( Das Debugging zeigt mir, dass "content" gefüllt ist, wenn es in den Constructor des SimpleAdapter gesetzt wird.

SirMArtin, etwas verzweifelnd
 
Zuletzt bearbeitet von einem Moderator:
Hi.

Versuch mal bitte dem ListView in der XML eine "normale" eigene ID zu geben und dann im Quellcode mit findViewById("id") eine Referenz auf deinen Listview zu bekommen (die entspr. Variable muss natürlich vom Typ ListView sein) und dann diesem ListView mit setAdapter deinen SimpleAdapter zuweisen.

Ich bin mir nicht sicher ob eine Activity, die von ListActivity erbt im Layout noch diverse andere Layout-Elemente haben kann/darf.

Gruß,
Shini
 
Hmm ich weiss nur ich hatte das auch mit der ID.
Kann die mal code von mir zeigen:

Das ganze ist eine Highscoreliste

hs.xml:
Code:
<AbsoluteLayout android:id="@+id/LinearLayout01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:screenOrientation = "portrait"
xmlns:android="http://schemas.android.com/apk/res/android">


<Button android:text="OK" android:layout_height="50px" android:layout_width="200px" android:layout_x="55px" android:layout_y="380px" android:id="@+id/HSButton"></Button>
<ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_y="35px" android:layout_height="330px"></ListView>
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="HIGHSCORE" android:textSize="20px" android:layout_x="100px" android:layout_y="5px" android:textColor="#ffff00"></TextView>
</AbsoluteLayout>

HsActivity.java
Code:
package com.android.blackjack;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;

public class HsActivity extends ListActivity implements OnClickListener
{    
    private BjDbAdapter database;

    
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.hs); 
        this.setRequestedOrientation(1);
        
        database = new BjDbAdapter(this);
        database.open();
        fillHighscore();
        database.close();
        
        Button b = (Button)this.findViewById(R.id.HSButton);
        b.setOnClickListener(this);
        
    }
    

    private void fillHighscore()
    {
        Cursor notesCursor = database.getHighscore();
        startManagingCursor(notesCursor);
        
        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{BjDbAdapter.KEY_NAME, BjDbAdapter.KEY_POINTS};
        
        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1, R.id.text2};
        
        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter notes = 
                new SimpleCursorAdapter(this, R.layout.hs_row, notesCursor, from, to);
        setListAdapter(notes);
    }

    public void onClick(View v) 
    {
        finish();
        
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {

        if(keyCode == KeyEvent.KEYCODE_BACK)
            return super.onKeyDown(KeyEvent.KEYCODE_ENTER, event);
        return super.onKeyDown(keyCode, event);
    }
}
 
Jetzt lade ich die ListView "result_list" vorher und setze den Adapter daran. Tut ebenso wenig. Spannend ist, dass einfach nix passiert. Keine Fehler meldung, kein nix.

Code:
ListView listview = (ListView) findViewById(R.id.result_list);
      SimpleAdapter adapter = new SimpleAdapter(this, content, R.layout.result_list_item, from, to);
      listview.setAdapter(adapter);

@Manfred: Der Unterschied, den ich zwischen Deinem und meinem Code sehe ist, dass Du einen SimpleCurserAdapter nutzt und ich einen SimpleAdapter, da meine Daten nicht aus der DB kommen sondern aus nem REST-Request.

SirMArtin
 
Kannst Du nochmal den ganzen Code schicken? Was sagt denn der Debugger?
 
Shinigami schrieb:
Versuch mal bitte dem ListView in der XML eine "normale" eigene ID zu geben und dann im Quellcode mit findViewById("id") eine Referenz auf deinen Listview zu bekommen (die entspr. Variable muss natürlich vom Typ ListView sein) und dann diesem ListView mit setAdapter deinen SimpleAdapter zuweisen.

Ich bin mir nicht sicher ob eine Activity, die von ListActivity erbt im Layout noch diverse andere Layout-Elemente haben kann/darf.
Hab ich versucht. Hat nichts verändert.

friedger schrieb:
Kannst Du nochmal den ganzen Code schicken? Was sagt denn der Debugger?
Im Originalpost ist der gesamte Code. Was fehlt Dir?

Der Debugger zeigt, dass die Liste in jedem Fall gefüllt ist. Das "setAdapter" habe ich nicht gedebuggt.

EDIT:
Manfred schrieb:
Nachdem ich das jetzt gelesen habe... Sieht ganz interessant aus. Werde es heute Abend mal ausprobieren...

SirMArtin
 
Zuletzt bearbeitet von einem Moderator:
Ein Versuch noch:

Dein TableLayout hat als "Höhe" fill_parent. Vllt bleibt dadurch schlicht kein Platz um den ListView anzuzeigen??!

Gruß,
Shini
 
  • Danke
Reaktionen: SirMArtin
Shinigami schrieb:
Ein Versuch noch:

Dein TableLayout hat als "Höhe" fill_parent. Vllt bleibt dadurch schlicht kein Platz um den ListView anzuzeigen??!
Sch**!§%$"! Copy&Paste. Das war's! Manchmal sieht man den Wald vor lauter Bäumen nicht.
 

Ähnliche Themen

A
Antworten
10
Aufrufe
1.016
swa00
swa00
FabianDev
Antworten
5
Aufrufe
547
swa00
swa00
D
Antworten
9
Aufrufe
1.762
jogimuc
J
Zurück
Oben Unten