gpt4 book ai didi

android - 如何从 XML 中定义的选项菜单打开新 View (调用 Activity )? (安卓)

转载 作者:行者123 更新时间:2023-11-29 22:34:57 25 4
gpt4 key购买 nike

我似乎无法从选项菜单项打开新 View 。该程序在将 Intent 和监听器应用于项目时不断崩溃。我才刚刚开始,所以请保持友善。

当前 View 是mnfsms,我试图打开的 View 是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;
}
}

list :

<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)

最佳答案

findViewById 调用替换为 menu.findItem

关于android - 如何从 XML 中定义的选项菜单打开新 View (调用 Activity )? (安卓),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2010701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com