简体   繁体   中英

Android ListActivity suddenly broke, can't find android.R.id.list even though its there

I realize that many of these questions floats around out there but I never found anyone with my exact problem.

As far as I can see, I'm doing it right, and it worked yesterday. Today I was working on other parts of my application and wanted to test it. Now I get this obnoxious error that it can't find the list with the id android.R.id.list, but again, it seems to be there.

Could any changes to other parts of the app have caused this, and in that case, how? Can anybody see the error, cause I sure can't.

I'm sorta desperate here since it's a school project and it's due soon. Thanks everybody, beforehand, for your time!

(in MainActivity.java)

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

(activity_main.xml)

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

</ListView>

LOGCAT as requested:

01-13 19:00:48.852: W/dalvikvm(25254): threadid=1: thread exiting with uncaught exception (group=0x40e6da08)
01-13 19:00:48.857: E/AndroidRuntime(25254): FATAL EXCEPTION: main
01-13 19:00:48.857: E/AndroidRuntime(25254): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.ajhansen.meetme/org.ajhansen.meetme.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-13 19:00:48.857: E/AndroidRuntime(25254):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at android.os.Looper.loop(Looper.java:158)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at android.app.ActivityThread.main(ActivityThread.java:5751)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at java.lang.reflect.Method.invokeNative(Native Method)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at java.lang.reflect.Method.invoke(Method.java:511)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at dalvik.system.NativeStart.main(Native Method)
01-13 19:00:48.857: E/AndroidRuntime(25254): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-13 19:00:48.857: E/AndroidRuntime(25254):    at android.app.ListActivity.onContentChanged(ListActivity.java:243)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:345)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at android.app.Activity.setContentView(Activity.java:1929)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at org.ajhansen.meetme.MainActivity.onCreate(MainActivity.java:72)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at android.app.Activity.performCreate(Activity.java:5165)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
01-13 19:00:48.857: E/AndroidRuntime(25254):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
01-13 19:00:48.857: E/AndroidRuntime(25254):    

... 11 more

A lot of people get errors with R file not generating properly etc., What I usually try as other's have suggested is the Clean action. If that doesn't work go through all your xml files and fine comb them cause any small error missing < or > can cause the R file to break. If this fails, delete the working project but keep the files on disk, then create a new project from existing code (assuming you're using eclipse) and find the path to the project folder. Make sure you tick copy to workspace and that usually solves my issues.

Your posted code does not seem to have any problem. It works fine. However, i was able to reproduce the same error as you got by changing the content of activity_main.xml with something different ie anything except ListView with @android:id/list as its id. Hence i would guess that somehow the correct layout file is not being fetched during setContentView . In order to check the content of the layout file, you may try replacing the onCreate code with this one,

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    XmlPullParser xpp = getResources().getLayout(R.layout.activity_main);
    int eventType;
    try {
        eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                Log.d("DEBUG_TAG", "Start tag " + xpp.getName());
            }
            eventType = xpp.next();
        }
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    setContentView(R.layout.activity_main);
}

Replace the "DEBUG_TAG" with your choice of tag to view the output in LogCat. If correct layout is fetched then you should get something similar to following in your LogCat.

01-14 00:43:00.671: D/DEBUG_TAG(29931): Start tag ListView

Try

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

Cal the list view like

ListView mlist = (ListView)findViewById(R.id.list);

Then clean your project and check.

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