2016-12-08 20 views
1

ボタンをクリックしてメインのアクティビティから私の "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)  
+0

@+id/startColorsを持っている必要があります 'ボタン=(ボタン)findViewById(R.id.startColors)を起動;'エラーが原因で発生しこの文。レイアウトに 'startColors' idを持つボタンがあることを確認してください。 – AlphaQ

+0

あなたのレイアウトに** colors **という名前の 'Button'がありますか? – emrekose26

+0

あなたのレイアウトを両方のアクティビティから表示します –

答えて

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

あなたは(それが何であれ)あなたの活動のレイアウトファイルに存在does notのボタンに到達しようとしているエラーによります。あなたのボタンのIDを確認し、それがあなたの "ColoursGame"アクティビティに関連するレイアウトページに置かれていることを確認してください。

+0

ヌルオブジェクト参照で 'Window.findViewById(int) '...ウィンドウがヌル。ボタンではない –

1

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

onCreatefindViewById外を呼び出さないでください、その時点でfindViewByIdを呼び出すための窓なしはありません。それでも発生する、NullPointerを取得する場合


、その後、あなたはactivity_colours_game.xml

Answer explanation here

関連する問題