gpt4 book ai didi

java - Android - 无法实例化 Activity - 应用已停止

转载 作者:行者123 更新时间:2023-11-29 04:28:24 25 4
gpt4 key购买 nike

我试图在两个 Activity 之间切换,但我的应用程序在运行时显示以下消息:“Myapp has stopped”。我已经创建了另一个 Activity 并将其放入 list 文件中。在我的主要 Activity 类中添加了以下鳕鱼:

public Button btnForgotPassword = (Button)findViewById(R.id.login);

public void init()
{
btnForgotPassword.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent itt = new Intent(LoginScreen.this, ForgotPassword.class);
startActivity(itt);
}
});
}

当我删除这段代码时它运行正常,但我真的很想切换 Activity 哈哈。

目录:

07-12 16:11:45.223 29065-29065/com.chest.chestbrasil.chest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.chest.chestbrasil.chest, PID: 29065
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.chest.chestbrasil.chest/com.chest.chestbrasil.chest.LoginScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2337)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:155)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:203)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.chest.chestbrasil.chest.LoginScreen.<init>(LoginScreen.java:13)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1090)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

最佳答案

public Button btnForgotPassword = (Button)findViewById(R.id.login);

您在调用 setContentView() 之前尝试访问小部件。

将此行更改为:

public Button btnForgotPassword;

然后,将此行添加到您的 onCreate() 方法中,在您的 setContentView() 调用之后:

btnForgotPassword = (Button)findViewById(R.id.login);

关于java - Android - 无法实例化 Activity - 应用已停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45066175/

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