自定义适配器错误ListView的id属性为'android.R.id.list'

时间:2013-12-25 22:43:45

标签: android xml android-listview android-adapter

我知道这已被问了很多,大多数答案都只是声明将xml文件修改为

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

但我仍然收到ID属性为ListView

的错误android.R.id.list

我的代码从本地数据库加载东西并将其作为ListView返回,请参阅下面的代码:

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;
    }

xml文件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> 

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>

让我知道你的建议,所有答案都表示赞赏!!

logcat错误:

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 个答案:

答案 0 :(得分:0)

将您的listview ID更改为

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

答案 1 :(得分:0)

我认为您必须删除工具ListActivity并将您的类创建为Activity。在此之后,您只需通过 listview 更改xml文件中id的{​​{1}}:

  

android:id =“@ + id / listview”

就像你试图在班上找到它(ListView)一样。


发生此问题是因为您尝试以两种不同的方式获取findViewById

  • 您尝试实施ListView,其中只有一个 ListActivity带有 android:list ID,并通过{{设置为适配器1}}。
  • 您还尝试通过ListView方法将setListAdapter设置为适配器。

因此,请保留实现方法并使用常规方法让ListView(查看herehere更改您的工具{{ 1}}并使用自定义setAdapter 这在此处继续:RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

希望这有帮助。

答案 2 :(得分:0)

您将列表视图称为android.R.id.list,但在代码中称为R.id.listview。在xml中,它被称为@android:id / list。在代码中使用R.id.list并将+添加到xml id。