前提・実現したいこと
問題なくビルドが出来ているのですが、アプリを起動した後ボタンを押して画面遷移したときにアプリが落ちてしまいます。
発生している問題・エラーメッセージ
アプリが落ちた時のlogcatの内容
2021-12-22 13:18:12.095 4280-4280/com.example.namaeate E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.namaeate, PID: 4280 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.namaeate/com.example.namaeate.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference at com.example.namaeate.MainActivity.<init>(MainActivity.java:27) at java.lang.Class.newInstance(Native Method) at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95) at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45) at android.app.Instrumentation.newActivity(Instrumentation.java:1253) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
該当のソースコード
StartMenu.java
public class StartMenu extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start); Button button = findViewById(R.id.StartButton); button.setOnClickListener((View v) -> { startActivity(new Intent(StartMenu.this, MainActivity.class)); }); } }
MainActivity.java
public class MainActivity extends AppCompatActivity { private TextView tvCount; private Button ansBtn1; private Button ansBtn2; private Button ansBtn3; private Button ansBtn4; public int prob = 5; //問題数 Intent intent = getIntent(); Integer i = intent.getIntExtra("i",0); Integer ans = intent.getIntExtra("ans",0); Integer count = intent.getIntExtra("count",0); //正解数 Integer backcount = intent.getIntExtra("backcount",0); //やり直し回数 // クイズデータの準備 String[][] quizData = { // {"正解", "選択肢1", "選択肢2", "選択肢3"} {"A1", "A2", "A3", "A4"}, {"B1", "B2", "B3", "B4"}, {"C1", "C2", "C3", "C4"}, {"D1", "D2", "D3", "D4"}, {"E1", "E2", "E3", "E4"}, {"F1", "F2", "F3", "F4"}, {"G1", "G2", "G3", "G4"}, {"H1", "H2", "H3", "H4"}, {"I1", "I2", "I3", "I4"}, {"J1", "J2", "J3", "J4"} }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getId(); showQuiz(); } public void getId(){ tvCount = findViewById(R.id.tvCount); ansBtn1 = findViewById(R.id.bt1); ansBtn2 = findViewById(R.id.bt2); ansBtn3 = findViewById(R.id.bt3); ansBtn4 = findViewById(R.id.bt4); } public void showQuiz(){ //シャッフル List<Integer> num = Arrays.asList(0, 1, 2, 3); Collections.shuffle(num); tvCount.setText( i ); ansBtn1.setText(quizData[i][num.get(0)]); ansBtn2.setText(quizData[i][num.get(1)]); ansBtn3.setText(quizData[i][num.get(2)]); ansBtn4.setText(quizData[i][num.get(3)]); } public void onButton(View view){ //押されたボタン Button clickedBtn =(Button)view; String clickedAns =clickedBtn.getText().toString(); if(clickedAns.equals(quizData[i][1])){ //正解 count++; ans = 0; }else{ //不正解 ans = 1; } Intent intent = new Intent(MainActivity.this,Answer.class); //Answerに送る変数 intent.putExtra("ans",ans); intent.putExtra("i",i); intent.putExtra("prob",prob); intent.putExtra("count",count); intent.putExtra("backcount",backcount); startActivity(intent); } }
Answer.java
public class Answer extends AppCompatActivity { private Button back; private Button next; //MainActivityから読み込み Intent intent = getIntent(); public int i = intent.getIntExtra("i",0); public int ans = intent.getIntExtra("ans",0); public int count = intent.getIntExtra("count",0); public int backcount = intent.getIntExtra("backcount",0); public int prob = intent.getIntExtra("prob",5); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_answer); getId(); showAnswer(); } public void getId(){ back = findViewById(R.id.BackButton); next = findViewById(R.id.NextButton); } public void showAnswer(){ ImageView Answer_picture = findViewById(R.id.Answer_picture); AssetManager assets = getResources().getAssets(); if(ans == 0){ //正解時 Answer_picture.setImageResource(R.drawable.maru); }else{ //不正解 Answer_picture.setImageResource(R.drawable.batsu); } } public void onNext(View view){ if(i == prob-1){ Intent intent = new Intent(Answer.this,result.class); //Resultに送る変数 intent.putExtra("count",count); intent.putExtra("backcount",backcount); intent.putExtra("prob",prob); startActivity(intent); }else{ i++; Intent intent = new Intent(Answer.this,MainActivity.class); //MainActivityに送る変数 intent.putExtra("i",i); intent.putExtra("count",count); intent.putExtra("backcount",backcount); startActivity(intent); } finish(); } public void onBack(View view){ backcount++; finish(); }
試したこと
manifest.xml
<activity android:name=".MainActivity" android:exported="true" android:screenOrientation="landscape" /> <activity android:name=".StartMenu" android:exported="true" android:screenOrientation="landscape" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
補足情報(FW/ツールのバージョンなど)
Androidstudioのバージョン
Android Studio Arctic Fox | 2020.3.1 Patch 4
Build #AI-203.7717.56.2031.7935034, built on November 21, 2021
ランタイム・バージョン: 11.0.10+0-b96-7249189 amd64
VM: OpenJDK 64-Bit Server VM by Oracle Corporation
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 6
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: org.jetbrains.kotlin
回答1件
あなたの回答
tips
プレビュー