Starting new activity from onOptionsItemSelected

a 夏天 提交于 2021-01-28 19:23:45

问题


I am trying to start a new activity(Settings Activity) from onOptionsItemSelected, but i am getting fatal exception when i click on option two it starts settings activity for a while than it crashes.

if i start Settings Activity from any where else than it starts without any error.

SettingsActivity

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



}

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

here is mainfest

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

log cat:

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.

回答1:


Caused by: java.lang.NullPointerException
04-19 03:59:34.679: E/AndroidRuntime(2733): at com.example.homeautomation.Room1.onStop(Room1.java:183)

Just set a breakpoint here (Room1.onStop) and find out what is null.




回答2:


Use something like this:

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



回答3:


While searching the web, I came across this, which is an alternative to what you`re trying to do.

You could have a base activity with 2 buttons( if you like ), one to take you to your main activity and another to take you to settings.



来源:https://stackoverflow.com/questions/23167647/starting-new-activity-from-onoptionsitemselected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!