1

在我的应用程序中,我想Dialog-box在按下时显示back button。但是 Logcat 给了我如下错误:

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 回答