在新活动之前关闭对话框

时间:2014-12-07 12:51:21

标签: android

我正在创建一个用户选择图像并创建拼图的游戏。第一个屏幕让用户选择一个图像,并在点击时出现一个对话框,让用户选择难度。在ImageSelection之后,图像在ShowImage活动中显示三秒钟,然后GamePlay启动。我按如下方式创建了对话框:

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

class GameDialog {

    private static Context mContext;
    private static int difficulty;
    static AlertDialog d;

    public static AlertDialog showDifficulties(Context c, final int img_id) {

        mContext = c;

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("Select difficulty")
               .setItems(R.array.my_array, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                   // the 'which' argument contains the index position
                   // of the selected item

                       // default difficulty is 3
                       int difficulty = 3;

                       // options for difficulties
                       switch (which) {

                           // when the user clicks 'easy'
                           case 0:
                               difficulty = 3;
                               System.out.print("DIFFICULTY");
                               System.out.println("" + difficulty);

                           break;

                           // when the user clicks 'medium'
                           case 1:
                           difficulty = 4;

                           System.out.print("DIFFICULTY");
                           System.out.println("" + difficulty);
                           break; 

                           // when the user clicks 'hard'
                           case 2:
                           difficulty = 5;

                           System.out.print("DIFFICULTY");
                           System.out.println("" + difficulty);
                           break;
                           }

                       // send intent with image id and difficulty 
                       // to ShowImage activity
                       Intent start_game = new Intent(mContext, ShowImage.class);
                       start_game.putExtra("img_id", img_id);
                       start_game.putExtra("difficulty", difficulty);
                       d.dismiss();
                       mContext.startActivity(start_game);
               }
        });

        d = builder.create();
        return d;

    }
}

这很顺利(并且有效)。其次,在GamePlay中,我希望用户能够从菜单中更改难度,之后应用程序再次启动ShowImage,但具有相应的选择难度。 以下是GamePlay的代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // handle item selection
    switch (item.getItemId()) {
        case R.id.reset_game:
            //TODO: resets the game to initial shuffled tiles
            Intent intent = getIntent();
            finish();
            startActivity(intent);

        case R.id.difficulty:
            // lets user change the difficulty of the game
            // pass the index to the dialog and show the dialog
            Dialog d = GameDialog.showDifficulties(GamePlay.this, selected_image);
            System.out.println(selected_image);
            d.show();

        case R.id.quit:
            // returns the user to ImageSelection
            intent = new Intent(GamePlay.this, ImageSelection.class);   
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
        default:
            return super.onOptionsItemSelected(item);
    }
}

然而,该应用程序抛出错误“GamePlay已泄露窗口com.android.internal.policy.impl.PhoneWindow $ DecorView(等),最初添加在这里” 我做了一些研究,并认为可能是因为没有关闭ImageSelection Activity中的对话框。所以我采取了一些措施并将其添加到我的 ImageSelection活动:

@Override
public void onPause()
{
    d.dismiss();
    super.onPause();

}

然而,这并没有解决我的问题:c 因此,我的问题是,我做错了什么以及如何修复错误?目标是在两个不同的活动中获得相同的对话框(来自我创建的单独的类)。 提前谢谢!

编辑:我现在添加了“d.dismiss();” Hany Elsioufy建议的对话类,但这并没有解决问题。

3 个答案:

答案 0 :(得分:1)

@覆盖 public boolean onOptionsItemSelected(MenuItem item){

// handle item selection
switch (item.getItemId()) {
    case R.id.reset_game:
        //TODO: resets the game to initial shuffled tiles
        Intent intent = getIntent();
        finish();
        startActivity(intent);

    case R.id.difficulty:
        // lets user change the difficulty of the game
        // pass the index to the dialog and show the dialog
        Dialog d = GameDialog.showDifficulties(GamePlay.this, selected_image);
        System.out.println(selected_image);
        d.show();

    case R.id.quit:
        // returns the user to ImageSelection
        intent = new Intent(GamePlay.this, ImageSelection.class);   
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    default:
        return super.onOptionsItemSelected(item);
}

}

在上面的代码中,没有break语句。 请在使用switch case时使用break语句。

答案 1 :(得分:0)

定义AlertDialog d;作为实例变量,然后在任何startActivity或Intents写入之前:

d.dismiss();

答案 2 :(得分:0)

我修好了!

原来我必须在onOptionsItemSelected()中的switch-case中添加return语句:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // handle item selection
    switch (item.getItemId()) {
        case R.id.reset_game:
            //TODO: resets the game to initial shuffled tiles
            Intent intent = getIntent();
            finish();
            startActivity(intent);
            return true;

        case R.id.difficulty:
            // lets user change the difficulty of the game
            // pass the index to the dialog and show the dialog
            Dialog d = GameDialog.showDifficulties(GamePlay.this, selected_image);
            System.out.println(selected_image);
            d.show();
            return true;

        case R.id.quit:
            // returns the user to ImageSelection
            intent = new Intent(GamePlay.this, ImageSelection.class);   
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
            return true;

        default: 
            return  super.onOptionsItemSelected(item);   

    }
}