У меня в Activity есть следующий код
View quizView = getLayoutInflater().inflate(R.layout.dialog_quiz_end, null);
TextView score = quizView.findViewById(R.id.quiz_result);
score.setText(getString(R.string.quizzfinalResults, correctAnswers, wrongAnswers));
AlertDialog.Builder dialog = new AlertDialog.Builder(Activity_Quiz.this);
dialog.setView(quizView)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
dialog.dismiss();
}
});
AlertDialog quizDialog = dialog.create();
quizDialog.show();
Также это XML макета dialog_quiz_end Это простой ContraintLayout с TextView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/quiz_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:ems="10"
android:gravity="center"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Это довольно просто , Я знаю. Но по какой-то причине у меня все еще возникает эта ошибка. Вот логарифм. Строка 331 - это quizDialog.show (). У меня sh кто угодно мог бы мне помочь.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.ViewGroup.findViewById(int)' on a null object reference
at com.android.internal.app.AlertController.setupButtons(AlertController.java:736)
at com.android.internal.app.AlertController.setupView(AlertController.java:533)
at com.android.internal.app.AlertController.installContent(AlertController.java:264)
at android.app.AlertDialog.onCreate(AlertDialog.java:436)
at android.app.Dialog.dispatchOnCreate(Dialog.java:407)
at android.app.Dialog.show(Dialog.java:302)
at com.example.geotrails.UI.Activity_Quiz.endOfQuiz(Activity_Quiz.java:311)
at com.example.geotrails.UI.Activity_Quiz.access$800(Activity_Quiz.java:39)
at com.example.geotrails.UI.Activity_Quiz$2$1.run(Activity_Quiz.java:249)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)