2014-12-07 105 views
-1

我正在创建一个游戏,其中用户选择图像并创建一个谜题。第一个屏幕让用户选择一个图像,并且点击时出现一个对话框,让用户选择难度。在ImageSelection之后,图像在ShowImage Activity中显示三秒钟,之后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,但是具有相应的选择难度。 下面是游戏的代码:

@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); 
    } 
} 

然而,应用程序引发错误“的游戏已泄漏(ETC),最初这里添加窗口com.android.internal.policy.impl.PhoneWindow $ DecorView” 我做了一些研究,并认为它可能是由于没有关闭ImageSelection Activity中的对话框而导致的。所以我采取了一些步骤,并已将此添加到我的ImageSelection活动:

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

} 

然而,这并没有解决我的问题:C 我的问题因此,我在做什么错了,我如何修正这个错误?目标是从两个不同的活动中获得同一个对话框(来自我创建的独立课程)。 在此先感谢!

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

+0

错误消息提到'PhoneWindow $ DecorView',但我不认为既不'PhoneWindow'也不' DecorView'在你的代码中。 – 2014-12-07 12:57:00

+0

我真的不知道它是什么意思,因为我在代码中看不到任何相关内容。但是当我谷歌搜索时,我发现它可能与不排除对话有关。 – Nifty 2014-12-07 13:00:21

回答

0

我修好了!

原来我不得不返回语句添加到我的switch-case中onOptionsItemSelected():

@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); 

    } 
} 
0

Define AlertDialog d;作为一个实例变量,然后之前的任何startActivity或意图写:

d.dismiss(); 
+0

你能解释一下吗?当我把它放在我的时候,让我们说GameActivity,我得到一个关于空指针引用的错误,因为它会在不存在的东西上调用“解除”。 – Nifty 2014-12-07 13:07:06

+0

你想在开始另一个活动之前关闭对话框,然后在一个实例变量中声明AlertDialog d,然后说AlertDialog d = builder.create();用d = builder.create()替换它。然后调用d.dismiss();之前startActivity()方法 – 2014-12-07 13:11:46

+0

我明白了。问题是,我的对话是在一个单独的课堂上。 startActivity也在该类中,这意味着如果我打电话给d。在该类中dismiss(),该对话将永远不会显示它在Activity中被调用的时间。我如何解决这个问题? – Nifty 2014-12-07 13:20:40

1

@覆盖 公共布尔onOptionsItemSelected(菜单项项){

// 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语句。

+0

哦!然后,我将什么放入onOptionsItemSelected返回?因为我明白这是一个布尔值。 – Nifty 2014-12-07 18:24:25

+0

在开关结束后添加此行返回super.onOptionsItemSelected(item) – kanivel 2014-12-07 18:28:32

+0

没关系,我找到了解决方案!感谢您让我走上正轨。 – Nifty 2014-12-07 18:29:38