问题描述
我的应用程序运行良好,直到我加入了以下code到选项菜单添加到我的主要活动:
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>
一切都编译,但我得到以下NullPointerException异常:
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
报告说,将有在
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)
但在该行的code只是工作罚款之前,如果我注释掉,出现在下面的一些线条同样的错误。这是没有意义的我。
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;
}
}
只是可以肯定,这里的的main.xml
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>
先谢谢了。
推荐答案
删除类R.java,让Eclipse中重新生成它解决它!
Deleting the class R.java and letting Eclipse regenerate it solved it!
我想对于任何理由R.java感到困惑的一些IDS的。
I think for any reason R.java was confused about some of the ids.
这篇关于RuntimeException的与Android OptionsMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!