0

I know this has been asked a lot and most answers simply state to modify xml file to

<ListView
  android:id="@android:id/list"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" >
</ListView>

but I'm still getting the error ListView whose id attribute is android.R.id.list

My code loads stuff from a local db and returns it as a ListView, see my code below:

public class PrepopSqliteDbActivity extends ListActivity {
    private static final String DB_NAME = "yourdb.sqlite3";
    //������� ��������� �������� ������� ���� ����� �� �����������
    private static final String TABLE_NAME = "friends";
    private static final String FRIEND_ID = "_id";
    private static final String FRIEND_NAME = "name";
    private static final String FRIEND_GAME = "game";
    public static final String[] ALL_KEYS = new String[] {FRIEND_ID, FRIEND_NAME, FRIEND_GAME};

    private SQLiteDatabase database;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //��� �������� ������
        ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
        database = dbOpenHelper.openDataBase();
        //���, ���� �������!
        populateListViewFromDB();

    }

    private void populateListViewFromDB() {
        Cursor cursor = getAllRows();

        // Allow activity to manage lifetime of the cursor.
        // DEPRECATED! Runs on the UI thread, OK for small/short queries.
        startManagingCursor(cursor);

        // Setup mapping from cursor to view fields:
        String[] fromFieldNames = new String[] 
                {FRIEND_ID, FRIEND_NAME, FRIEND_GAME, };
        int[] toViewIDs = new int[]
                {    R.id.item_icon,   R.id.item_name,        R.id.item_game};

        // Create adapter to may columns of the DB onto elemesnt in the UI.
        @SuppressWarnings("deprecation")
        SimpleCursorAdapter myCursorAdapter = 
                new SimpleCursorAdapter(
                        this,       // Context
                        R.layout.item_layout,   // Row layout template
                        cursor,                 // cursor (set of DB records to map)
                        fromFieldNames,         // DB Column names
                        toViewIDs               // View IDs to put information in
                        );

        // Set the adapter for the list view
        ListView myList = (ListView) findViewById(R.id.listview);
        myList.setAdapter(myCursorAdapter);
    }
    public Cursor getAllRows() {
        String where = null;
        Cursor c =  database.query(true, TABLE_NAME, ALL_KEYS, 
                            where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

the xml file activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp">

<ListView
  android:id="@android:id/list"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" >
</ListView> 

</LinearLayout> 

and item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/item_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:maxHeight="80dp"
        android:maxWidth="80dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="NAME!"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/item_game"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/item_name"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />



</RelativeLayout>

Let me know your suggestions, all answers are appreciated!!

logcat error:

12-25 18:12:20.319: E/AndroidRuntime(902): FATAL EXCEPTION: main
12-25 18:12:20.319: E/AndroidRuntime(902): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.softeq.android.prepopdb/com.softeq.prepopdb.activity.PrepopSqliteDbActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-25 18:12:20.319: E/AndroidRuntime(902):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-25 18:12:20.319: E/AndroidRuntime(902):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-25 18:12:20.319: E/AndroidRuntime(902):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-25 18:12:20.319: E/AndroidRuntime(902):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-25 18:12:20.319: E/AndroidRuntime(902):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-25 18:12:20.319: E/AndroidRuntime(902):  at android.os.Looper.loop(Looper.java:137)
12-25 18:12:20.319: E/AndroidRuntime(902):  at android.app.ActivityThread.main(ActivityThread.java:5103)
12-25 18:12:20.319: E/AndroidRuntime(902):  at java.lang.reflect.Method.invokeNative(Native Method)
12-25 18:12:20.319: E/AndroidRuntime(902):  at java.lang.reflect.Method.invoke(Method.java:525)
12-25 18:12:20.319: E/AndroidRuntime(902):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-25 18:12:20.319: E/AndroidRuntime(902):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-25 18:12:20.319: E/AndroidRuntime(902):  at dalvik.system.NativeStart.main(Native Method)
12-25 18:12:20.319: E/AndroidRuntime(902): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-25 18:12:20.319: E/AndroidRuntime(902):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
12-25 18:12:20.319: E/AndroidRuntime(902):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
12-25 18:12:20.319: E/AndroidRuntime(902):  at android.app.Activity.setContentView(Activity.java:1895)
12-25 18:12:20.319: E/AndroidRuntime(902):  at com.softeq.prepopdb.activity.PrepopSqliteDbActivity.onCreate(PrepopSqliteDbActivity.java:35)
12-25 18:12:20.319: E/AndroidRuntime(902):  at android.app.Activity.performCreate(Activity.java:5133)
12-25 18:12:20.319: E/AndroidRuntime(902):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-25 18:12:20.319: E/AndroidRuntime(902):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-25 18:12:20.319: E/AndroidRuntime(902):  ... 11 more

3 Answers3

0

Change your listview id to

<ListView
  android:id="@+id/listview"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" >
</ListView> 
TootsieRockNRoll
  • 3,218
  • 2
  • 25
  • 50
0

I think you must to remove the implements ListActivity and create your class as an Activity. After this, you just have to change your id of your ListView in your xml file by listview:

android:id="@+id/listview"

Like you tried to find it (findViewById) in your class.


This problem happens because you try to get a ListView with two different ways.

  • You try to implements ListActivity, which need only one ListView with the android:list id and set to an adapter by setListAdapter.
  • And you also try to have your ListView set to your adapter by setAdapter method.

So, keep the implement method and use the normal way to have a ListActivity (look here and here) OR change your implement by Activity and use a custom ListView.
This is resumed here: RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

Hope this helps.

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
0

You are referring to the listview as android.R.id.list, but then in the code as R.id.listview. In the xml, its called @android:id/list. UseR.id.list in your code and add the + to the xml id.

cmike
  • 51
  • 6