我似乎无法从选项菜单项打开新视图。程序在将意图和侦听器应用于项目时不断崩溃。我才刚刚开始,所以请善待。
当前视图是mnfsms,而我试图打开的视图是mnfsms_settings。我正在为 1.5 开发。
有人可以帮我让菜单正常工作吗?
菜单(称为 options_menu.xml):
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/settings_button"
android:title="Settings"
android:icon="@android:drawable/ic_menu_preferences" />
<item android:id="@+id/about_button"
android:title="About"
android:icon="@android:drawable/ic_menu_myplaces" />
</menu>
主视图(称为 mnfsms.java):
package com.example.mnfsms;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class mnfsms extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* OnClickListener myocl = new View.OnClickListener() {
public void onClick(View v){
Intent myi = new Intent(mnfsms.this, mnfsms_settings.class);
startActivity(myi);
}
};*/
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
MenuItem mi_settings = (MenuItem)findViewById(R.id.settings_button);
mi_settings.setIntent(new Intent(this, mnfsms_settings.class));
return true;
}
}
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mnfsms"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".mnfsms" android:label="@string/main_window_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".mnfsms_settings" android:label="string/main_window_name">
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
堆栈跟踪:
01-06 15:07:58.045: ERROR/AndroidRuntime(2123): Uncaught handler: thread main exiting due to uncaught exception
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): java.lang.NullPointerException
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at com.example.mnfsms.mnfsms.onCreateOptionsMenu(mnfsms.java:30)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at android.app.Activity.onCreatePanelMenu(Activity.java:2038)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:421)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:664)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1278)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1735)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2188)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2158)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at android.view.ViewRoot.handleMessage(ViewRoot.java:1490)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at android.os.Looper.loop(Looper.java:123)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at android.app.ActivityThread.main(ActivityThread.java:3948)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at java.lang.reflect.Method.invoke(Method.java:521)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
01-06 15:07:58.055: ERROR/AndroidRuntime(2123): at dalvik.system.NativeStart.main(Native Method)