执行Intent语句时我的应用程序崩溃了

时间:2017-08-23 18:31:00

标签: java android android-intent

我学习android studio编程,我的程序有问题, 当我想使用intent语句进入新活动我的应用程序崩溃了, 这是我的主要活动;

public class MainActivity extends AppCompatActivity {

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

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

    mAuth = FirebaseAuth.getInstance();
}

public void onStart(){
    super.onStart();
    FirebaseUser CurrentUser = mAuth.getCurrentUser();
    if(CurrentUser == null){
        Intent StartIntent = new Intent(MainActivity.this, 
                                     StartActivity.class);
        startActivity(StartIntent);
        finish();
    }
  }

}

这很明显:

android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".StartActivity"
        android:label="@string/app_name">

    </activity>

没有错误消息 但这就是我在android监视器中所拥有的:

08-23 21:50:33.507 11264-11264/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.ruse.ayse.areyousmartenough, PID: 11264
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ruse.ayse.areyousmartenough/com.ruse.ayse.areyousmartenough.StartActivity}: java.lang.NullPointerException
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2378)
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:155)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:136)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5433)
                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                                                   at dalvik.system.NativeStart.main(Native Method)
                                                Caused by: java.lang.NullPointerException
                                                   at com.ruse.ayse.areyousmartenough.StartActivity.onCreate(StartActivity.java:31)
                                                   at android.app.Activity.performCreate(Activity.java:5301)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2291)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2378) 
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:155) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:136) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5433) 
                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:515) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
                                                   at dalvik.system.NativeStart.main(Native Method) 

我通过添加&#34; intent-filter&#34;将此问题解决到另一个项目中进入清单文件,但它不适用于这个项目,

这是StartActivity:

public class StartActivity extends AppCompatActivity {

private Button mRegBtn ;
private Button mLoginBtn;

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

    mRegBtn = (Button) findViewById(R.id.RegBtn);
    mLoginBtn = (Button) findViewById(R.id.LoginBtn);

    mRegBtn.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            Intent createAccount = new Intent(StartActivity.this, RegisterActivity.class);
            startActivity(createAccount);
        }
    });
    mLoginBtn.setOnClickListener(new View.OnClickListener(){  /////this is line 31
        @Override
        public void onClick(View view) {
            Intent loginAccount = new Intent(StartActivity.this, LoginActivity.class);
            startActivity(loginAccount);
        }
    });
}}

1 个答案:

答案 0 :(得分:2)

你的问题出在StartActivity.java:31,你试图从空引用中读取。

根据您的评论,您希望错过从Layout =

获取mLoginBtn
mLoginBtn = (Button) findViewById(R.id.btn_login);