我正在研究一段可以使用 Menu inflater 切换图像的代码,我已经研究过代码,但是一旦我单击按钮,它就会给我强制关闭错误。这是我的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.cameraa:
Toast.makeText(this, "Start Camera!", Toast.LENGTH_LONG).show();
break;
case R.id.gallery:
Toast.makeText(this, "this is gallery!", Toast.LENGTH_LONG).show();
break;
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.ghost_gallery:
switcher.showNext();
break
}
return true;
}
主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewSwitcher
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<FrameLayout
android:id="@+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:scaleType="matrix"
android:src="@drawable/banana" />
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:scaleType="matrix"
android:src="@drawable/apple" />
</FrameLayout>
</ViewSwitcher>
这是我的日志
05-21 03:19:35.733: E/AndroidRuntime(758): FATAL EXCEPTION: main
05-21 03:19:35.733: E/AndroidRuntime(758): java.lang.NullPointerException
05-21 03:19:35.733: E/AndroidRuntime(758): at org.example.touch.Touch.nextView(Touch.java:188)
05-21 03:19:35.733: E/AndroidRuntime(758): at org.example.touch.Touch.onOptionsItemSelected(Touch.java:180)
05-21 03:19:35.733: E/AndroidRuntime(758): at android.app.Activity.onMenuItemSelected(Activity.java:2195)
05-21 03:19:35.733: E/AndroidRuntime(758): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
05-21 03:19:35.733: E/AndroidRuntime(758): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
05-21 03:19:35.733: E/AndroidRuntime(758): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
05-21 03:19:35.733: E/AndroidRuntime(758): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
05-21 03:19:35.733: E/AndroidRuntime(758): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
05-21 03:19:35.733: E/AndroidRuntime(758): at android.view.View$PerformClick.run(View.java:8816)
05-21 03:19:35.733: E/AndroidRuntime(758): at android.os.Handler.handleCallback(Handler.java:587)
05-21 03:19:35.733: E/AndroidRuntime(758): at android.os.Handler.dispatchMessage(Handler.java:92)
05-21 03:19:35.733: E/AndroidRuntime(758): at android.os.Looper.loop(Looper.java:123)
05-21 03:19:35.733: E/AndroidRuntime(758): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-21 03:19:35.733: E/AndroidRuntime(758): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 03:19:35.733: E/AndroidRuntime(758): at java.lang.reflect.Method.invoke(Method.java:521)
05-21 03:19:35.733: E/AndroidRuntime(758): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-21 03:19:35.733: E/AndroidRuntime(758): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-21 03:19:35.733: E/AndroidRuntime(758): at dalvik.system.NativeStart.main(Native Method)
请就此事帮助我....最好的问候,