BadTokenException: невозможно добавить окно - токен android.os.BinderProxy@98fa673 недействителен;ваша деятельность работает? - PullRequest
0 голосов
/ 21 октября 2019

TextView count, trye, timer;

Button btn_one, btn_two, btn_three, btn_four,back;
ImageView tv_question;
int counter = 1;
int next = 1;
int tries = 3;
CountDownTimer yourCountDownTimer;
private Animalquestions question = new Animalquestions();
private String answer;
private int questionLength = question.questions.length;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_animalquiz);

    count = findViewById(R.id.count);
    btn_one = (Button) findViewById(R.id.btn_one);
    btn_one.setOnClickListener(this);
    btn_two = (Button) findViewById(R.id.btn_two);
    btn_two.setOnClickListener(this);
    btn_three = (Button) findViewById(R.id.btn_three);
    btn_three.setOnClickListener(this);
    btn_four = (Button) findViewById(R.id.btn_four);
    trye = findViewById(R.id.tries);
    timer = findViewById(R.id.timer);
    btn_four.setOnClickListener(this);
    back = findViewById(R.id.back);

    tv_question = (ImageView) findViewById(R.id.tv_question);

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
            return;
        }
    });
    yourCountDownTimer = new CountDownTimer(15000, 1000) {

        public void onTick(long millisUntilFinished) {
            timer.setText("" + millisUntilFinished / 1000);
        }

        public void onFinish() {
            TryAgain();

        }

    };
    NextQuestion(0);
    yourCountDownTimer.start();

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_one:
            if (btn_one.getText() == answer) {
                Toast.makeText(Animalquiz.this, "You Are Correct", Toast.LENGTH_SHORT).show();
                if (next >= questionLength) {
                    Finished();
                } else {
                    yourCountDownTimer.cancel();
                    yourCountDownTimer.start();
                    NextQuestion(next);
                    next++;
                    counter++;
                    trye.setText(" Tries  " + String.valueOf(tries));
                    count.setText("Score  " + String.valueOf(counter));
                }
            } else if (tries <= 0) {
                GameOver();
            } else {
                TryAgain();
            }

            break;

        case R.id.btn_two:
            if (btn_two.getText() == answer) {
                Toast.makeText(Animalquiz.this, "You Are Correct", Toast.LENGTH_SHORT).show();
                if (next >= questionLength) {
                    Finished();
                } else {
                    yourCountDownTimer.cancel();
                    yourCountDownTimer.start();
                    NextQuestion(next);
                    next++;
                    counter++;
                    trye.setText(" Tries  " + String.valueOf(tries));
                    count.setText("Score  " + String.valueOf(counter));
                }
            } else if (tries <= 0) {
                GameOver();
            } else {
                TryAgain();
            }

            break;

        case R.id.btn_three:
            if (btn_three.getText() == answer) {
                Toast.makeText(Animalquiz.this, "You Are Correct", Toast.LENGTH_SHORT).show();
                if (next >= questionLength) {
                    Finished();
                } else {
                    yourCountDownTimer.cancel();
                    yourCountDownTimer.start();
                    NextQuestion(next);
                    next++;
                    counter++;
                    trye.setText(" Tries  " + String.valueOf(tries));
                    count.setText("Score  " + String.valueOf(counter));
                }
            } else if (tries <= 0) {
                GameOver();
            } else {
                TryAgain();
            }

            break;

        case R.id.btn_four:
            if (btn_four.getText() == answer) {
                Toast.makeText(Animalquiz.this, "You Are Correct", Toast.LENGTH_SHORT).show();
                if (next >= questionLength) {
                    Finished();
                } else {
                    yourCountDownTimer.cancel();
                    yourCountDownTimer.start();
                    NextQuestion(next);
                    next++;
                    counter++;
                    trye.setText(" Tries  " + String.valueOf(tries));
                    count.setText("Score  " + String.valueOf(counter));
                }
            } else if (tries <= 0) {
                GameOver();
            } else {
                TryAgain();
            }
            break;
    }
}

private void TryAgain() {
    yourCountDownTimer.cancel();
    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Animalquiz.this);
    alertDialogBuilder
            .setMessage("You have " + tries + " tries left")
            .setCancelable(false)
            .setPositiveButton("try again", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    yourCountDownTimer.start();
                    tries--;

Fruitquiz.class));//}

                }
            })
            .setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    System.exit(0);
                }
            });
    alertDialogBuilder.show();
}

private void GameOver() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Animalquiz.this);
    yourCountDownTimer.cancel();
    alertDialogBuilder
            .setMessage("Game Over /n your total score is " + (String.valueOf(counter)))
            .setCancelable(false)
            .setPositiveButton("New Game", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    startActivity(new Intent(getApplicationContext(), Animalquiz.class));
                }
            })
            .setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    System.exit(0);
                }
            });
    alertDialogBuilder.show();

}

private void Finished() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Animalquiz.this);
    yourCountDownTimer.cancel();
    alertDialogBuilder
            .setMessage("Game Finished your total score is " + (String.valueOf(counter)))
            .setCancelable(false)
            .setPositiveButton("New Game", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    startActivity(new Intent(getApplicationContext(), Animalquiz.class));
                }
            })
            .setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    System.exit(0);
                }
            });
    alertDialogBuilder.show();

}

private void NextQuestion(int num) {
    tv_question.setImageResource(question.getQuestion(num));
    btn_one.setText(question.getchoice1(num));
    btn_two.setText(question.getchoice2(num));
    btn_three.setText(question.getchoice3(num));
    btn_four.setText(question.getchoice4(num));

    answer = question.getCorrectAnswer(num);

}

2019-10-21 21: 48: 33.020 26853-26853 / com.whatthemobile.quizforkids E / AndroidRuntime: ИСКЛЮЧИТЕЛЬНОЕ ИСКЛЮЧЕНИЕ: основной Процесс: com.whatthemobile.quizforkids, PID: 26853android.view.WindowManager $ BadTokenException: невозможно добавить окно - токен android.os.BinderProxy@98fa673 недействителен;ваша деятельность работает? на android.view.ViewRootImpl.setView (ViewRootImpl.java:806) на android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:356) на android.view.WindowManagerImpl.addView (WindowManagerImpl.java:94.)Dialog.show (Dialog.java:329) в android.app.AlertDialog $ Builder.show (AlertDialog.java:1125) в com.whatthemobile.quizforkids.Animalquiz.TryAgain (Animalquiz.java:193) в com.whatthemobile.quizfork.Animalquiz.access $ 000 (Animalquiz.java:15) в com.whatthemobile.quizforkids.Animalquiz $ 2.onFinish (Animalquiz.java:64) в android.os.CountDownTimer $ 1.handleMessage (CountDownTimer.java:127) в android.os.Handler.dispatchMessage (Handler.java:107) на android.os.Looper.loop (Looper.java:198) на android.app.ActivityThread.main (ActivityThread.java:6729) на java.lang.reflect.Method. invoke (собственный метод) в com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run (RuntimeInit.java:493) в com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...