在应用程序中获取运行时异常

时间:2010-12-30 05:59:27

标签: android listview nullpointerexception

我在我的应用程序中遇到了运行时异常,我已经尝试了很多来解决它,但是无法做到。

以下是我收到错误的代码:

public class FirstDemo extends ListActivity
{
    private ListView Lv02;
    private String Lv_Arr02[]={ "darshan", "sunil", "viral", "jaimin", "aashish", "jaydev", "bhavesh"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylistview);  ( Here I am getting the Exception )
        Lv02=(ListView) findViewById(R.id.lstvw_mylist);
}
}

My Stack Trace is as under:


12-30 11:21:09.525: ERROR/AndroidRuntime(517): FATAL EXCEPTION: main
12-30 11:21:09.525: ERROR/AndroidRuntime(517): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.mytabhostdemo/com.android.mytabhostdemo.FirstDemo}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.widget.TabHost.setCurrentTab(TabHost.java:323)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.view.View.performClick(View.java:2408)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.view.View$PerformClick.run(View.java:8816)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.os.Handler.handleCallback(Handler.java:587)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.os.Looper.loop(Looper.java:123)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at java.lang.reflect.Method.invokeNative(Native Method)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at java.lang.reflect.Method.invoke(Method.java:521)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at dalvik.system.NativeStart.main(Native Method)
12-30 11:21:09.525: ERROR/AndroidRuntime(517): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.app.ListActivity.onContentChanged(ListActivity.java:245)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.app.Activity.setContentView(Activity.java:1647)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at com.android.mytabhostdemo.FirstDemo.onCreate(FirstDemo.java:17)  (Here I think)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-30 11:21:09.525: ERROR/AndroidRuntime(517):     ... 18 more

如果有人能帮助我,那就太好了。 谢谢, 大卫

1 个答案:

答案 0 :(得分:1)

在您的XML文件中,您的top元素必须是ListView,因为您派生自ListActivity。

示例:

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

当从特定活动派生时,Android期望在布局XML文件中有一个具有预定义ID的特定定义。

如果您发布XML,我们可以为您解决。