This question already has answers here:
RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

(7个答案)


6年前关闭。





我正在使用onclicklistener,使用按钮时应用崩溃了...
当我使用唯一按钮时,崩溃发生在首页上。
无法找到问题...
这是mainActivity:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button)findViewById(R.id.button1);
        final EditText ed = (EditText)findViewById(R.id.editText1);
        final String str = ed.getText().toString();
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, NotesPage.class);
                intent.putExtra("username", str);
                startActivity(intent);
            }
        });

    }
}


和notespage:

public class NotesPage extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notespage);
        Button btn = (Button)findViewById(R.id.button1);
        TextView tx = (TextView)findViewById(R.id.textView2);
        Intent intent = getIntent();
        final String str = intent.getStringExtra("username");
        tx.setText("Hello"+str);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NotesPage.this, Addnote.class);
                startActivity(intent);
            }
        });
    }

}


表现:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.noteslist.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="NotesPage"></activity>
    <activity android:name="Addnote">
    </activity>
</application>


Logcat:

11-18 19:53:43.755: D/AndroidRuntime(1159): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-18 19:53:43.755: D/AndroidRuntime(1159): CheckJNI is ON
11-18 19:53:44.905: D/AndroidRuntime(1159): Calling main entry com.android.commands.pm.Pm
11-18 19:53:44.965: D/AndroidRuntime(1159): Shutting down VM
11-18 19:53:44.986: D/dalvikvm(1159): GC_CONCURRENT freed 101K, 78% free 463K/2048K, paused 1ms+1ms
11-18 19:53:44.986: D/dalvikvm(1159): Debugger has detached; object registry had 1 entries
11-18 19:53:44.995: I/AndroidRuntime(1159): NOTE: attach of thread 'Binder Thread #3' failed
11-18 19:53:46.175: D/AndroidRuntime(1172): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-18 19:53:46.175: D/AndroidRuntime(1172): CheckJNI is ON
11-18 19:53:47.395: D/AndroidRuntime(1172): Calling main entry com.android.commands.am.Am
11-18 19:53:47.435: I/ActivityManager(91): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.noteslist/.MainActivity} from pid 1172
11-18 19:53:47.466: W/WindowManager(91): Failure taking screenshot for (180x300) to layer 21005
11-18 19:53:47.505: D/AndroidRuntime(1172): Shutting down VM
11-18 19:53:47.535: D/dalvikvm(1182): Not late-enabling CheckJNI (already on)
11-18 19:53:47.535: D/dalvikvm(1172): GC_CONCURRENT freed 102K, 77% free 485K/2048K, paused 1ms+2ms
11-18 19:53:47.545: D/dalvikvm(1172): Debugger has detached; object registry had 1 entries
11-18 19:53:47.555: I/AndroidRuntime(1172): NOTE: attach of thread 'Binder Thread #3' failed
11-18 19:53:47.575: I/ActivityManager(91): Start proc com.example.noteslist for activity com.example.noteslist/.MainActivity: pid=1182 uid=10042 gids={}
11-18 19:53:47.945: W/NetworkManagementSocketTagger(91): setKernelCountSet(10042, 1) failed with errno -2
11-18 19:53:48.507: I/Process(91): Sending signal. PID: 1182 SIG: 3
11-18 19:53:48.507: I/dalvikvm(1182): threadid=3: reacting to signal 3
11-18 19:53:48.675: I/dalvikvm(1182): Wrote stack traces to '/data/anr/traces.txt'
11-18 19:53:48.955: I/Process(91): Sending signal. PID: 1182 SIG: 3
11-18 19:53:48.955: I/dalvikvm(1182): threadid=3: reacting to signal 3
11-18 19:53:48.996: I/dalvikvm(1182): Wrote stack traces to '/data/anr/traces.txt'
11-18 19:53:49.205: D/gralloc_goldfish(1182): Emulator without GPU emulation detected.
11-18 19:53:49.275: I/ActivityManager(91): Displayed com.example.noteslist/.MainActivity: +1s789ms (total +9m33s430ms)
11-18 19:53:49.345: W/NetworkManagementSocketTagger(91): setKernelCountSet(10013, 0) failed with errno -2
11-18 19:53:50.705: D/dalvikvm(91): GREF has increased to 501
11-18 19:53:51.605: D/dalvikvm(143): GC_CONCURRENT freed 3991K, 30% free 9843K/13959K, paused 7ms+17ms
11-18 19:53:53.545: D/InputEventConsistencyVerifier(1182): KeyEvent: ACTION_UP but key was not down.
11-18 19:53:53.545: D/InputEventConsistencyVerifier(1182):   in android.widget.EditText@412bcf98
11-18 19:53:53.545: D/InputEventConsistencyVerifier(1182):   0: sent at 1735495000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=1735495, downTime=1735362, deviceId=0, source=0x101 }
11-18 19:53:55.975: I/ActivityManager(91): START {cmp=com.example.noteslist/.NotesPage (has extras)} from pid 1182
11-18 19:53:56.014: W/WindowManager(91): Failure taking screenshot for (180x300) to layer 21010
11-18 19:53:56.304: D/AndroidRuntime(1182): Shutting down VM
11-18 19:53:56.304: W/dalvikvm(1182): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
11-18 19:53:56.354: E/AndroidRuntime(1182): FATAL EXCEPTION: main
11-18 19:53:56.354: E/AndroidRuntime(1182): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.noteslist/com.example.noteslist.NotesPage}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
11-18 19:53:56.354: E/AndroidRuntime(1182):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at android.os.Looper.loop(Looper.java:137)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at android.app.ActivityThread.main(ActivityThread.java:4424)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at java.lang.reflect.Method.invokeNative(Native Method)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at java.lang.reflect.Method.invoke(Method.java:511)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at dalvik.system.NativeStart.main(Native Method)
11-18 19:53:56.354: E/AndroidRuntime(1182): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
11-18 19:53:56.354: E/AndroidRuntime(1182):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at android.app.Activity.setContentView(Activity.java:1835)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at com.example.noteslist.NotesPage.onCreate(NotesPage.java:15)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at android.app.Activity.performCreate(Activity.java:4465)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-18 19:53:56.354: E/AndroidRuntime(1182):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
11-18 19:53:56.354: E/AndroidRuntime(1182):     ... 11 more
11-18 19:53:56.514: W/ActivityManager(91):   Force finishing activity com.example.noteslist/.NotesPage
11-18 19:53:56.535: W/ActivityManager(91):   Force finishing activity com.example.noteslist/.MainActivity
11-18 19:53:56.674: I/Process(91): Sending signal. PID: 1182 SIG: 3
11-18 19:53:56.694: I/dalvikvm(1182): threadid=3: reacting to signal 3
11-18 19:53:56.754: I/dalvikvm(1182): Wrote stack traces to '/data/anr/traces.txt'
11-18 19:53:57.044: W/ActivityManager(91): Activity pause timeout for ActivityRecord{4143f3e0 com.example.noteslist/.NotesPage}
11-18 19:53:57.055: I/Process(91): Sending signal. PID: 1182 SIG: 3
11-18 19:53:57.064: I/dalvikvm(1182): threadid=3: reacting to signal 3
11-18 19:53:57.094: I/dalvikvm(1182): Wrote stack traces to '/data/anr/traces.txt'
11-18 19:53:57.364: W/NetworkManagementSocketTagger(91): setKernelCountSet(10013, 1) failed with errno -2
11-18 19:53:57.607: D/dalvikvm(91): GC_CONCURRENT freed 458K, 11% free 11479K/12807K, paused 12ms+73ms
11-18 19:53:57.834: W/NetworkManagementSocketTagger(91): setKernelCountSet(10042, 0) failed with errno -2
11-18 19:54:02.674: I/Process(1182): Sending signal. PID: 1182 SIG: 9
11-18 19:54:02.824: I/ActivityManager(91): Process com.example.noteslist (pid 1182) has died.
11-18 19:54:02.834: W/InputDispatcher(91): channel '416022e0 com.example.noteslist/com.example.noteslist.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
11-18 19:54:02.845: E/InputDispatcher(91): channel '416022e0 com.example.noteslist/com.example.noteslist.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
11-18 19:54:02.915: I/WindowManager(91): WIN DEATH: Window{416022e0 com.example.noteslist/com.example.noteslist.MainActivity paused=true}
11-18 19:54:02.915: W/InputDispatcher(91): Attempted to unregister already unregistered input channel '416022e0 com.example.noteslist/com.example.noteslist.MainActivity (server)'
11-18 19:54:02.974: I/WindowManager(91): WINDOW DIED Window{416022e0 com.example.noteslist/com.example.noteslist.MainActivity paused=true}
11-18 19:54:03.004: W/InputManagerService(91): Got RemoteException sending setActive(false) notification to pid 1182 uid 10042
11-18 19:54:03.717: D/dalvikvm(143): GC_CONCURRENT freed 421K, 30% free 9887K/13959K, paused 7ms+42ms
11-18 19:54:07.860: W/ActivityManager(91): Activity destroy timeout for ActivityRecord{41556b68 com.example.noteslist/.MainActivity}
11-18 19:54:07.860: W/ActivityManager(91): Activity destroy timeout for ActivityRecord{4143f3e0 com.example.noteslist/.NotesPage}

最佳答案

您的NotesPage是扩展ListActivity而不是Activity,并且y0u在NotesPage中没有任何listview

因此,将扩展名从ListActivity更改为Activity

08-06 13:59