从onOptionsItemSelected启动新活动

时间:2014-04-19 08:13:55

标签: java android android-activity

我正在尝试从onOptionsItemSelected启动一个新活动(设置活动),但是当我点击选项二时,它会开始设置活动一段时间而不是崩溃,我会遇到致命的异常。

如果我从其他任何地方开始设置活动而没有任何错误。

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



}

第一项活动(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>

这里是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.

3 个答案:

答案 0 :(得分:4)

使用类似的东西:

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

答案 1 :(得分:0)

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

只需在此设置breakpoint (Room1.onStop),找出什么是空。

答案 2 :(得分:-2)

在搜索网络时,我遇到了this,这是您尝试做的事情的替代方案。

您可以使用2个按钮进行基本活动(如果您愿意),一个可以带您进入主要活动,另一个可以带您进行设置。