gpt4 book ai didi

java - 从 onOptionsItemSelected 开始新 Activity

转载 作者:行者123 更新时间:2023-11-29 21:10:11 24 4
gpt4 key购买 nike

我正在尝试从 onOptionsItemSelected 启动一个新 Activity (设置 Activity ),但是当我单击选项二时出现致命异常,它启动设置 Activity 一段时间后崩溃。

如果我从其他任何地方启动设置 Activity 而不是它启动时没有任何错误。

设置 Activity

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class SettingsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}



}

第一个 Activity (Room1.java):

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room1);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflator = getMenuInflater();
inflator.inflate(R.menu.room1, menu);
return true;
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {

if(item.getItemId()==R.id.menu_option_two){
Settings();
Log.d("Option","2");

}
return super.onOptionsItemSelected(item);
}

private void Settings() {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent) ;
}

room1.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:icon="@android:drawable/ic_menu_share"
android:title="Option 1"
android:id="@+id/menu_option_one"
/>

<item
android:icon="@android:drawable/ic_menu_preferences"
android:title="Option 2"
android:id="@+id/menu_option_two"
/>

<item
android:icon="@android:drawable/ic_menu_info_details"
android:title="Option 3"
android:id="@+id/menu_option_three"
/>

</menu>

这是主要的

  <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.homeautomation.Splash"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.homeautomation.Main"
android:label="@string/title_activity_main"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
</activity>
<activity
android:name="com.example.homeautomation.Room1"
android:label="@string/title_activity_room1"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
</activity>
<activity
android:name="com.example.homeautomation.DemoMSG"
android:label="@string/title_activity_demo_msg"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
</activity>
<activity
android:name="com.example.homeautomation.SettingsActivity"
android:label="@string/title_activity_settings"
>
</activity>
</application>

日志猫:

04-19 03:59:28.179: I/Choreographer(2733): Skipped 100 frames!  The application may be doing too much work on its main thread.
04-19 03:59:32.849: D/Option(2733): 2
04-19 03:59:34.659: D/AndroidRuntime(2733): Shutting down VM
04-19 03:59:34.659: W/dalvikvm(2733): threadid=1: thread exiting with uncaught exception (group=0xb4a1eb90)
04-19 03:59:34.679: E/AndroidRuntime(2733): FATAL EXCEPTION: main
04-19 03:59:34.679: E/AndroidRuntime(2733): Process: com.example.homeautomation, PID: 2733
04-19 03:59:34.679: E/AndroidRuntime(2733): java.lang.RuntimeException: Unable to stop activity {com.example.homeautomation/com.example.homeautomation.Room1}: java.lang.NullPointerException
04-19 03:59:34.679: E/AndroidRuntime(2733): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3169)
04-19 03:59:34.679: E/AndroidRuntime(2733): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3215)
04-19 03:59:34.679: E/AndroidRuntime(2733): at android.app.ActivityThread.access$1000(ActivityThread.java:135)
04-19 03:59:34.679: E/AndroidRuntime(2733): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
04-19 03:59:34.679: E/AndroidRuntime(2733): at android.os.Handler.dispatchMessage(Handler.java:102)
04-19 03:59:34.679: E/AndroidRuntime(2733): at android.os.Looper.loop(Looper.java:137)
04-19 03:59:34.679: E/AndroidRuntime(2733): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-19 03:59:34.679: E/AndroidRuntime(2733): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 03:59:34.679: E/AndroidRuntime(2733): at java.lang.reflect.Method.invoke(Method.java:515)
04-19 03:59:34.679: E/AndroidRuntime(2733): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-19 03:59:34.679: E/AndroidRuntime(2733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-19 03:59:34.679: E/AndroidRuntime(2733): at dalvik.system.NativeStart.main(Native Method)
04-19 03:59:34.679: E/AndroidRuntime(2733): Caused by: java.lang.NullPointerException
04-19 03:59:34.679: E/AndroidRuntime(2733): at com.example.homeautomation.Room1.onStop(Room1.java:183)
04-19 03:59:34.679: E/AndroidRuntime(2733): at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1212)
04-19 03:59:34.679: E/AndroidRuntime(2733): at android.app.Activity.performStop(Activity.java:5388)
04-19 03:59:34.679: E/AndroidRuntime(2733): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3166)
04-19 03:59:34.679: E/AndroidRuntime(2733): ... 11 more
04-19 03:59:37.299: I/Process(2733): Sending signal. PID: 2733 SIG: 9
04-19 03:59:38.729: D/dalvikvm(2760): GC_FOR_ALLOC freed 35K, 4% free 2716K/2828K, paused 34ms, total 35ms
04-19 03:59:38.729: I/dalvikvm-heap(2760): Grow heap (frag case) to 3.192MB for 491416-byte allocation
04-19 03:59:38.779: D/dalvikvm(2760): GC_FOR_ALLOC freed <1K, 4% free 3195K/3308K, paused 42ms, total 42ms
04-19 03:59:38.999: D/gralloc_goldfish(2760): Emulator without GPU emulation detected.

最佳答案

使用这样的东西:

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_option_two:

Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;


}

return super.onOptionsItemSelected(item);
}

关于java - 从 onOptionsItemSelected 开始新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23167647/

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