1

私のアプリケーションでは、Dialog-boxを押したときに表示したいのですが、 back buttonLogcatは次のようなエラーを出します:

E/WindowManager( 3044): Activity com.MAT.Canadian.Immi.Test.Play.Display_questions has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4053e1c8 that was originally added here
E/WindowManager( 3044): android.view.WindowLeaked: Activity com.MAT.Canadian.Immi.Test.Play.Display_questions has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4053e1c8 that was originally added here

私の場合、Dialog-Boxその後数秒間、Ok/cancelボタンを押さなくても表示されます。どうしてこうなったのかわからないので、どこが間違っているのか教えてください。

  @Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    Toast.makeText(getApplicationContext(), "close the quiz app", Toast.LENGTH_SHORT).show();

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage("Are you sure you want to exit?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    Display_questions.this.finish();
                   //dialog.dismiss();

               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           });
    AlertDialog alert = builder.create();
    alert.show();
}

私のコード全体を確認してください:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz_questions);

        listview = (ListView) findViewById(R.id.questions_list);
        listview.setItemsCanFocus(false);

        GoToNextQuestion();
}

private void GoToNextQuestion() {
    // TODO Auto-generated method stub


    listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {


                String  selectedFromList = (String) listview.getItemAtPosition(pos);

                SelectedAnswer.setAnswer(selectedFromList);

                if (!checkAnswer()) return;

                if (currentGame.isGameOver()){

                    Intent i = new Intent(Display_questions.this, Display_result.class);
                    i.putExtra("Timer_Value", TimerTime);
                    startActivity(i); 

                    finish();
                }
                else{

                    GoToNextQuestion();


                } 
            }
          });

     setQuestions();
  }

 private void setQuestions() {

    String question = currentQ.getQuestion().trim();
    TextView qText = (TextView) findViewById(R.id.txt_questions);
    qText.setText(question);

    // set the available options
    List<String> answers = currentQ.getQuestionOptions();

     adapter = new ListviewAdapter(this,answers);

     listview.setAdapter(adapter);

}

private boolean checkAnswer() {
    //getSelectedAnswer();

    // logic for the check answers
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    Toast.makeText(getApplicationContext(), "close the quiz app", Toast.LENGTH_SHORT).show();

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage("Are you sure you want to exit?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    Display_questions.this.finish();
                   //dialog.dismiss();

               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           });
    AlertDialog alert = builder.create();
    alert.show();
}

 }

どうもありがとう。

4

1 に答える 1

3

onBackPressed() をこれに変更します

@Override
public void onBackPressed() {
//  super.onBackPressed();
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage("Are you sure you want to exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    MyActivity.this.finish();
                    //dialog.dismiss();

                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();

}
于 2012-09-15T12:40:21.630 に答える