RuntimeException with Android OptionsMenu
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 me开发者_运维技巧nu) {
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.
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.
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.
精彩评论