Android - 无法实例化活动 - 应用已停止

时间:2017-07-12 19:36:18

标签: java android

我试图在两个活动之间切换,但我的应用程序在运行时显示以下消息:" Myapp已停止"。我已经创建了另一个活动并将其放在清单文件中。在我的主要活动类中添加了以下鳕鱼:

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);
        }
    });
}

当我删除此代码时,它运行正常,但我真的想切换活动哈哈。

目录下载:

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) 

1 个答案:

答案 0 :(得分:3)

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

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

将此行更改为:

public Button btnForgotPassword;

然后,在onCreate()来电后,将此行添加到setContentView()方法中:

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