gpt4 book ai didi

java - 面临主要 Activity 困难

转载 作者:行者123 更新时间:2023-12-02 01:40:29 26 4
gpt4 key购买 nike

我在尝试运行我的应用程序时遇到一些错误,这里是 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

这是我的主要 Activity `公共(public)类 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) {

}

最佳答案

Activity 不需要实例化,因此您只需调用以下代码即可启动 Activity :

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

基本上你只需要删除这一行:

new MainActivity(intent);

并启动 Activity :

startActivity(intent);

关于java - 面临主要 Activity 困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54503667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com