Android 对话框窗口。应用程序崩溃

时间:2021-04-01 00:36:33

标签: java android button dialog

我想使用 Dialog 类为测验应用程序制作一个预览弹出窗口。我创建了一个对话框并想要 将 setOnClickListener() 放在它的 button 上,它应该关闭对话框。但是当我添加 onClickListener 应用程序停止工作时,如果我评论 onClickListener() 函数它会起作用。如何为对话框窗口的按钮添加 onClickListener?

        previewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

dialog.dismiss();
            }
        });
        nextQuestion();
    }

对话框xml:

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="410dp"
    android:background="@color/black75"
    android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="30"
        android:textSize="28dp"
        android:layout_gravity="center_horizontal"
        android:textColor="@color/white"
        android:layout_marginBottom="25dp"
        android:text="Kirish"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:textAlignment="center"
        android:layout_weight="80"
        android:textSize="20sp"
        android:textColor="@color/white"
        android:text="Islom dini haqidagi bilimingizni sinab ko'ring.Siz uchun tayyorlab qo'yilgan 10 ta savolni nechtasiga javob berishingizni tekshirib ko'ring"/>
</LinearLayout>
    <Button
        android:id="@+id/previewButton"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:textColor="@color/black"
        android:text="Start"
        android:layout_weight="100"
        android:textSize="24sp"
        android:background="@drawable/question_background"
        android:padding="20dp"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="50dp"
        android:layout_gravity="center"
        />

</LinearLayout>

java代码:

   package com.example.islomniorganamiz;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Random;

public class SavolJavob extends AppCompatActivity implements View.OnClickListener {
    Random random=new Random();
    int questionNumber=1;
    TextView questionTextView,whichQuestion;
    Button option1,option2,option3,option4;
    Button previewButton;
    Questions questions=new Questions();
    ArrayList<Integer> askedQuestions = new ArrayList<Integer>();
Dialog dialog;
    int random_button1;
    int random_button2;
    int random_button3;
    int random_button4;
    int random_number;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_savol_javob);
        questionTextView=findViewById(R.id.questionTextView);
        whichQuestion=findViewById(R.id.whichQuestion);
        option1=findViewById(R.id.option1);
        option2=findViewById(R.id.option2);
        option3=findViewById(R.id.option3);
        option4=findViewById(R.id.option4);
        previewButton=findViewById(R.id.previewButton);


        option1.setOnClickListener(this);
        option2.setOnClickListener(this);
        option3.setOnClickListener(this);
        option4.setOnClickListener(this);

        dialog=new Dialog(this);
        dialog.setContentView(R.layout.previewdialog);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        dialog.show();
        previewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

dialog.dismiss();
            }
        });
        nextQuestion();
    }


    @Override
    public void onClick(View v) {
        String correctAnswer=questions.correctAnswersArray[random_number];
        switch (v.getId()){
                case R.id.option1:
                    if (option1.getText().equals(correctAnswer)){
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                        option1.setBackgroundResource(R.drawable.option_correct);

                            }
                        },100);

                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                option1.setBackgroundResource(R.drawable.option_normal);
                            }
                        },800);
                 }
                    else{
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                option1.setBackgroundResource(R.drawable.option_incorrect);
                            }
                        },100);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                option1.setBackgroundResource(R.drawable.option_normal);
                            }
                        },800);
                    }
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            nextQuestion();
                        }
                    },1200);
                   break;

                case R.id.option2:
                if (option2.getText().equals(correctAnswer)){
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            option2.setBackgroundResource(R.drawable.option_correct);
                        }
                    },100);

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            option2.setBackgroundResource(R.drawable.option_normal);
                        }
                    },800);
                }else{
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            option2.setBackgroundResource(R.drawable.option_incorrect);
                        }
                    },100);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            option2.setBackgroundResource(R.drawable.option_normal);
                        }
                    },800);
                }
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            nextQuestion();
                        }
                    },1200);
                break;

            case R.id.option3:
                if (option3.getText().equals(correctAnswer)){
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            option3.setBackgroundResource(R.drawable.option_correct);
                        }
                    },100);

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            option3.setBackgroundResource(R.drawable.option_normal);
                        }
                    },800);
                }else{
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            option3.setBackgroundResource(R.drawable.option_incorrect);
                        }
                    },100);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            option3.setBackgroundResource(R.drawable.option_normal);
                        }
                    },800);
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        nextQuestion();
                    }
                },1200);
                break;

            case R.id.option4:
                if (option4.getText().equals(correctAnswer)){
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            option4.setBackgroundResource(R.drawable.option_correct);
                        }
                    },100);

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            option4.setBackgroundResource(R.drawable.option_normal);
                        }
                    },800);
                }else{
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            option4.setBackgroundResource(R.drawable.option_incorrect);
                        }
                    },100);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            option4.setBackgroundResource(R.drawable.option_normal);
                        }
                    },800);
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        nextQuestion();
                    }
                },1200);
                break;

        }

    }




    public void nextQuestion(){

       random_number=random.nextInt(questions.questionsArray.length);
        while ((askedQuestions.contains(random_number))){
           random_number=random.nextInt(questions.questionsArray.length);
       }
       askedQuestions.add(random_number);
        random_button1=random.nextInt(4);
        while (random_button1==random_button2) {
            random_button2 = random.nextInt(4);
        }
        while (random_button3==random_button1||random_button3==random_button2) {
            random_button3 = random.nextInt(4);
        }
        while (random_button4==random_button1||random_button4==random_button2||random_button4==random_button3) {
            random_button4 = random.nextInt(4);
       }
        whichQuestion.setText(questionNumber+" - SAVOL");
     questionTextView.setText(questions.questionsArray[random_number]);
     option1.setText(questions.optionsArray[random_number][random_button1]);
     option2.setText(questions.optionsArray[random_number][random_button2]);
     option3.setText(questions.optionsArray[random_number][random_button3]);
     option4.setText(questions.optionsArray[random_number][random_button4]);



questionNumber++;
    }


}

1 个答案:

答案 0 :(得分:1)

请在您的代码中添加这一行

previewButton = dialog.findViewById(R.id.previewButton);

这里是完整的代码

 dialog=new Dialog(this);
 dialog.setContentView(R.layout.previewdialog);
//add previewButton in this place
previewButton = dialog.findViewById(R.id.previewButton);
相关问题