我正在創建一個遊戲,其中用戶選擇圖像並創建一個謎題。第一個屏幕讓用戶選擇一個圖像,並且點擊時出現一個對話框,讓用戶選擇難度。在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建議的對話類,但是這並沒有解決問題。
錯誤消息提到'PhoneWindow $ DecorView',但我不認爲既不'PhoneWindow'也不' DecorView'在你的代碼中。 – 2014-12-07 12:57:00
我真的不知道它是什麼意思,因爲我在代碼中看不到任何相關內容。但是當我谷歌搜索時,我發現它可能與不排除對話有關。 – Nifty 2014-12-07 13:00:21