Я просмотрел все решения, размещенные в стеке, связанные с этой проблемой, но, похоже, ни одно из них не работает для меня. Вот мой код для моей MainActivity:
package com.quizappproject.android.quizappproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int score = 0;
TextView answerStatus = findViewById(R.id.answerStatus);
TextView scoreKeeper = findViewById(R.id.scoreKeeper);
public void checkAnswer(View view){
CheckBox lionCheckBox = findViewById(R.id.lion);
CheckBox rhinoCheckBox = findViewById(R.id.rhino);
CheckBox leopardCheckBox = findViewById(R.id.leopard);
if (lionCheckBox.isChecked() && rhinoCheckBox.isChecked() && leopardCheckBox.isChecked()) {
score = score + 1;
scoreKeeper.setText(String.valueOf(score));
answerStatus.setText("CORRECT");
}else{
score = score + 0;
scoreKeeper.setText(String.valueOf(score));
answerStatus.setText("WRONG, TRY AGAIN");
}
}
}
Вот мой logcat:
06-30 19:11:54.765 23172-23172/com.quizappproject.android.quizappproject E/ActivityThread: Exception when newActivity r=ActivityRecord{15ec5572 token=android.os.BinderProxy@1fd54dc3 {com.quizappproject.android.quizappproject/com.quizappproject.android.quizappproject.MainActivity}} token=android.os.BinderProxy@1fd54dc3
06-30 19:11:54.765 23172-23172/com.quizappproject.android.quizappproject D/AndroidRuntime: Shutting down VM
06-30 19:11:54.768 23172-23172/com.quizappproject.android.quizappproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.quizappproject.android.quizappproject, PID: 23172
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.quizappproject.android.quizappproject/com.quizappproject.android.quizappproject.MainActivity}:
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:2457)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2632)
at android.app.ActivityThread.access$700(ActivityThread.java:183)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1485)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5674)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
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:117)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:56)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:202)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.quizappproject.android.quizappproject.MainActivity.<init>(MainActivity.java:18)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2632)
at android.app.ActivityThread.access$700(ActivityThread.java:183)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1485)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5674)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
06-30 19:11:54.770 880-1725/? W/ActivityManager: Force finishing activity 1 com.quizappproject.android.quizappproject/.MainActivity
Когда я запускаю свой код на реальном устройстве Android, он падает, и я получаю вышеуказанную ошибку в своем logcat. Я отключил мгновенный запуск в моих настройках, но приложение все еще ломается, когда я запускаю его. В чем может быть проблема с моим кодом?