在 mainActivity 的 onCreate 中启动另一个 activity 会产生错误

Starting another activity in onCreate of mainActivity generates error

我正在构建一个简单的应用程序(在每次启动时)获取首选项并检查用户是否同意条款。如果不是,它会开始另一个 activity 条款。这是我开始的代码 activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences preferences = getPreferences(MODE_PRIVATE);
        boolean termsConfirmed = preferences.getBoolean("termsConfirmed", false);
        if(!termsConfirmed) {
            Intent intent = new Intent(this, confirmTerms.class);
            startActivityForResult(intent, TERMS_CONFIRMATION);
        }
}

然而,这会导致此方法中出现空指针异常:

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        refresh = menu.findItem(R.id.action_refresh);
        listener.refresh = refresh;
}

已满 logcat 错误代码:

02-03 09:15:20.577: E/AndroidRuntime(1996): FATAL EXCEPTION: main
02-03 09:15:20.577: E/AndroidRuntime(1996): java.lang.NullPointerException
02-03 09:15:20.577: E/AndroidRuntime(1996):     at hzs.sk.hzs.MainActivity.onCreateOptionsMenu(MainActivity.java:141)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at android.app.Activity.onCreatePanelMenu(Activity.java:2158)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at android.support.v7.app.ActionBarActivityDelegate.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:1056)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1259)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at android.support.v7.app.ActionBarActivityDelegateBase.access0(ActionBarActivityDelegateBase.java:80)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at android.support.v7.app.ActionBarActivityDelegateBase.run(ActionBarActivityDelegateBase.java:119)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at android.os.Handler.handleCallback(Handler.java:587)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at android.os.Looper.loop(Looper.java:130)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at java.lang.reflect.Method.invoke(Method.java:507)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-03 09:15:20.577: E/AndroidRuntime(1996):     at dalvik.system.NativeStart.main(Native Method)

如果我开始 activity 而没有结果,例如 startActivity() 并通过调用 finish() 完成它,那么它可以工作,但我需要那个 activity 的结果,所以我必须开始和我一样。有什么想法吗?

感谢转发

不知道是什么原因exception.But如果你需要第二个Activity的结果,你可以将结果保存为另一个class的静态字段的值(也是静态字段的首先 Activity),然后先完成 Activity 并首先恢复 Activity,使用该字段的值。