简体   繁体   中英

Android: Content must have a ListView whose id attribute is 'android.R.id.list

I have tired every possible solution online (including Stackoverflow) to fix this error, however nothing works any help would be much appreciated.

What is the Error?

java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

Note: There is many answers on stack overflow and I have tired them all.

What do I have in my Code?

XML:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Framelayout to display Fragments -->
<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        />


<!-- Listview to display slider menu -->

<ListView

    android:id="@android:id/list"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"        
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>

Code:

 @SuppressLint("NewApi") public class MainActivity extends FragmentActivity {

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

mDrawerList = (ListView) this.findViewById(android.R.id.list);

Anyone know how to fix this error?

Complete logcat report

09-19 14:53:00.376: E/MainActivity(28935): Error in creating fragment
09-19 14:53:00.387: D/AndroidRuntime(28935): Shutting down VM
09-19 14:53:00.394: E/AndroidRuntime(28935): FATAL EXCEPTION: main
09-19 14:53:00.394: E/AndroidRuntime(28935): Process: info.androidhive.slidingmenu, PID: 28935
09-19 14:53:00.394: E/AndroidRuntime(28935): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.app.ActivityThread.access$800(ActivityThread.java:148)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.os.Looper.loop(Looper.java:135)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.app.ActivityThread.main(ActivityThread.java:5312)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at java.lang.reflect.Method.invoke(Native Method)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at java.lang.reflect.Method.invoke(Method.java:372)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
09-19 14:53:00.394: E/AndroidRuntime(28935): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1243)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.app.Activity.performStart(Activity.java:5969)
09-19 14:53:00.394: E/AndroidRuntime(28935):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277)
09-19 14:53:00.394: E/AndroidRuntime(28935):    ... 10 more

do not use android.R.id.list instead use R.id.list.
Sorry for previous half answer but @android:id/list is specific to ListActivity and ListFragment from documentation:

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)

so either use @+id/list and then R.id.list or extend your Activity with ListActivity Generally +id is used to create the new id in R while android:id is used in xml to customize with our own view as mentioned in the documentation. Hence I am saying @android:id/list is specific to ListActivity and ListFragment.

只需 jusr 从您的 MainActivity setContentView(R.layout.activity_main) 中删除一行;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM