2014-12-07 118 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