Android OptionsMenu 的 RuntimeException

发布于 2024-10-19 22:06:09 字数 5132 浏览 5 评论 0原文

我的应用程序运行良好,直到我添加以下代码以将选项菜单添加到我的主活动中:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.wantlist_menu, menu);
        return true;
}

菜单资源如下所示:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:enabled="true" android:visible="true" android:id="@+id/clearImageCashe"   android:title="@string/clearImageCache"></item>
</menu>

一切都在编译,但我得到以下 NullpointerException:

03-03 19:11:09.001: ERROR/AndroidRuntime(341): java.lang.RuntimeException: Unable to   start activity      ComponentInfo{de.kosmowski.discogs.wants/de.kosmowski.discogs.wants.activities.wantlist}:       java.lang.NullPointerException
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.os.Looper.loop(Looper.java:123)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at java.lang.reflect.Method.invoke(Method.java:521)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at dalvik.system.NativeStart.main(Native Method)
03-03 19:11:09.001: ERROR/AndroidRuntime(341): Caused by: java.lang.NullPointerException
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at de.kosmowski.discogs.wants.activities.wantlist.onCreate(wantlist.java:49)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     ... 11 more

处将出现 NullPointer 异常

Caused by: java.lang.NullPointerException
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at de.kosmowski.discogs.wants.activities.wantlist.onCreate(wantlist.java:49)

它说在但代码 在这行之前工作得很好,如果我将其注释掉,下面的某些行会出现相同的错误。这对我来说毫无意义。

我只是不明白这里发生了什么事。

最后但并非最不重要的是完整的活动类(没有导入):

public class wantlist extends Activity implements OnClickListener {
/** Called when the activity is first created. */

ArrayList<Want> wants = new ArrayList<Want>();

@Override
public void onCreate(Bundle savedInstanceState) {        

    wants.add(new Want("Want1"));
    wants.add(new Want("Want2"));
    wants.add(new Want("Want3"));
    wants.add(new Want("Want4"));
    wants.add(new Want("Want5"));

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    ListView lv = (ListView) findViewById(R.id.WantList); 
    ImageButton b = (ImageButton) findViewById(R.id.searchbutton);
    b.setOnClickListener(this); //NullPointerException here


    lv.setAdapter(new WantAdapter(this,
            R.layout.lplistitem, wants));
    lv.setTextFilterEnabled(true);

}

public void onClick(View v) {
    onSearchRequested();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.wantlist_menu, menu);
        return true;
}

}

只是为了确保这里是 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="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageButton android:gravity="right" android:layout_alignParentRight="true"   android:src="@drawable/searchbutton" android:text="@string/search" android:id="@+id/searchbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="visible"></ImageButton>
</RelativeLayout>

<ListView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/WantList"></ListView>
</LinearLayout>

提前致谢。

My application was running fine until i added the following code to add an options Menu to my main Activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.wantlist_menu, menu);
        return true;
}

The Menu Resource looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:enabled="true" android:visible="true" android:id="@+id/clearImageCashe"   android:title="@string/clearImageCache"></item>
</menu>

Everything is compiling but i get the following NullpointerException:

03-03 19:11:09.001: ERROR/AndroidRuntime(341): java.lang.RuntimeException: Unable to   start activity      ComponentInfo{de.kosmowski.discogs.wants/de.kosmowski.discogs.wants.activities.wantlist}:       java.lang.NullPointerException
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.os.Looper.loop(Looper.java:123)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at java.lang.reflect.Method.invoke(Method.java:521)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at dalvik.system.NativeStart.main(Native Method)
03-03 19:11:09.001: ERROR/AndroidRuntime(341): Caused by: java.lang.NullPointerException
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at de.kosmowski.discogs.wants.activities.wantlist.onCreate(wantlist.java:49)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     ... 11 more

It says there would be a NullPointer Exception at

Caused by: java.lang.NullPointerException
03-03 19:11:09.001: ERROR/AndroidRuntime(341):     at de.kosmowski.discogs.wants.activities.wantlist.onCreate(wantlist.java:49)

But the Code at this line just worked fine before and if i comment it out, the same error appears at some lines below. That makes no sense to me.

I just can't figure out what is going on here.

Last but not least the complete activity class (without imports):

public class wantlist extends Activity implements OnClickListener {
/** Called when the activity is first created. */

ArrayList<Want> wants = new ArrayList<Want>();

@Override
public void onCreate(Bundle savedInstanceState) {        

    wants.add(new Want("Want1"));
    wants.add(new Want("Want2"));
    wants.add(new Want("Want3"));
    wants.add(new Want("Want4"));
    wants.add(new Want("Want5"));

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    ListView lv = (ListView) findViewById(R.id.WantList); 
    ImageButton b = (ImageButton) findViewById(R.id.searchbutton);
    b.setOnClickListener(this); //NullPointerException here


    lv.setAdapter(new WantAdapter(this,
            R.layout.lplistitem, wants));
    lv.setTextFilterEnabled(true);

}

public void onClick(View v) {
    onSearchRequested();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.wantlist_menu, menu);
        return true;
}

}

Just to be sure here's the 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="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageButton android:gravity="right" android:layout_alignParentRight="true"   android:src="@drawable/searchbutton" android:text="@string/search" android:id="@+id/searchbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="visible"></ImageButton>
</RelativeLayout>

<ListView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/WantList"></ListView>
</LinearLayout>

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫林﹌晚霞¤ 2024-10-26 22:06:09

main.xml 中是否存在 id searchbutton

如果在布局中找不到 id,您将返回一个空指针。如果 id 存在于另一个布局文件中,编译器将不会捕获这一点。

Does the id searchbutton exist in main.xml?

You would get back a null pointer if the id couldn't be found in your layout. The compiler won't catch this if the id exists in another layout file.

勿忘初心 2024-10-26 22:06:09

删除 R.java 类并让 Eclipse 重新生成它就解决了!

我认为出于某种原因,R.java 对某些 id 感到困惑。

Deleting the class R.java and letting Eclipse regenerate it solved it!

I think for any reason R.java was confused about some of the ids.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文