面对主要活动的困难

时间:2019-02-03 14:05:10

标签: java android

尝试运行我的应用时遇到一些错误,这里是logcat

  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.chatapp/com.example.chatapp.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.example.chatapp.MainActivity> has no zero argument constructor
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.InstantiationException: java.lang.Class<com.example.chatapp.MainActivity> has no zero argument constructor

这是我的主要主动性 公共类MainActivity扩展了AppCompatActivity {

Button login, register;

FirebaseUser firebaseUser;

@Override
protected void onStart() {
    super.onStart();

    firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

    //check if user in null
    if (firebaseUser != null){
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        new MainActivity(intent);
        finish();
    }

}

`

但我也认为这是一个错误

 public MainActivity(Intent intent) {

}

1 个答案:

答案 0 :(得分:2)

不需要实例化活动,因此您只需调用以下内容即可开始活动:

//check if user in null
if (firebaseUser != null){
    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
    startActivity(intent);
    finish();
}

基本上,您只需要删除以下行:

new MainActivity(intent);

然后通过以下方式开始活动:

startActivity(intent);