通过intent启动活动时出现NullPointerException

时间:2015-01-13 18:12:38

标签: java android

我有以下代码,我正在努力开始一项活动,因为某些条件属实。但是startActivity(controllerActivity); java.lang.NullPointerException我得到了controllerActivity

阶段的Intent { act=android.intent.action.Main }变量包含有效值Thread thr_authenticateUser = new Thread(new Runnable() { @Override public void run() { … Boolean _authenticationStatus = Boolean.valueOf(authenticationReplyValue); if (_authenticationStatus) { Intent controllerActivity = new Intent("android.intent.action.Main"); startActivity(controllerActivity); //NullPointerException } } }); thr_authenticateUser.start();

  protected void onCreate(final Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.main_view); // HERE: java.lang.NullPointerException

在进一步的调查结果中,我发现它失败的确切位置是主要活动的'onCreate()

main_view

但视图名称Manifest:是正确的并且存在。 <activity android:name=".Main" android:label="@string/app_name" android:launchMode="singleInstance" android:windowSoftInputMode="adjustPan" > <intent-filter> <action android:name="android.intent.action.Main" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>

java.lang.NullPointerException
        at android.app.Activity.startActivityForResult(Activity.java:3370)
        at android.app.Activity.startActivityForResult(Activity.java:3331)
        at android.app.Activity.startActivity(Activity.java:3566)
        at android.app.Activity.startActivity(Activity.java:3534)
        at com.citypulse.citypulse.User$1.run(User.java:57)
        at java.lang.Thread.run(Thread.java:856)

Compelte stack trace:

{{1}}

1 个答案:

答案 0 :(得分:-1)

获取开始活动的背景

getBaseContext().startActivity(controlledActivity);

YourActivity.this.startActivity(controlledActivity);