新活动在启动时崩溃

时间:2016-12-08 21:08:14

标签: java android nullpointerexception

当点击按钮从主Activity切换到我的“ColoursGame”活动时,应用程序崩溃。

我还是初学者,所以不是调试专家。

主要活动代码

Button colorsGame = (Button) findViewById(R.id.colours);
            colorsGame.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(MainActivity.this, ColoursGame.class));
                }
            });

显示新活动

<activity android:name=".ColoursGame"
        android:label="ColourGame"
        android:theme="@style/AppTheme.NoActionBar"></activity>

ColoursGame活动OnCreate代码

public class ColoursGame extends Activity {

int livesCount = 3;
String x;

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

    Button start = (Button) findViewById(R.id.startColors);
    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            vSwitch.showNext();
            x = String.valueOf(livesCount);
            lives.setText(x);
            text();
            textColor();
            backgroundColor();

            start();
        }
    });

}

错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.aynaar.numbersgameforkids, PID: 3278
              java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.aynaar.numbersgameforkids/com.aynaar.numbersgameforkids.ColoursGame}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                  at android.app.Activity.findViewById(Activity.java:2323)
                  at com.aynaar.numbersgameforkids.ColoursGame.<init>(ColoursGame.java:42)
                  at java.lang.Class.newInstance(Native Method)
                  at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

2 个答案:

答案 0 :(得分:1)

  

at com.aynaar.numbersgameforkids.ColoursGame.<init>(ColoursGame.java:42)

请勿在{{1​​}}之外致电findViewById,此时无法调用onCreate的窗口。

如果您仍然获得NullPointer,那么您必须findViewById中有@+id/startColors

Answer explanation here

答案 1 :(得分:0)

Button start = (Button) findViewById(R.id.startColors);

根据错误,您尝试访问活动布局文件中不存在的按钮(无论它是什么)。检查按钮的ID,确保它位于与您的&#34; ColoursGame&#34;相关的布局页面上。活性。