사용자가 이미지를 선택하고 퍼즐이 만들어지는 게임을 만들고 있습니다. 첫 번째 화면에서는 사용자가 이미지를 선택하고 클릭 할 때 나타나는 대화 상자가 표시되어 사용자가 어려움을 선택할 수 있습니다. ImageSelection을 실행하면 ShowImage 활동에 3 초 동안 이미지가 표시되고 그 후 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);
}
}
그러나, 응용 프로그램 오류 "게임 플레이 화면이 com.android.internal.policy.impl.PhoneWindow $ DecorView이 (등) 원래 여기에 추가 된 유출했다"던졌습니다 : 여기 게임 플레이 코드입니다 나는 약간의 연구를했고 ImageSelection 활동에서 대화 상자를 닫지 않아서 발생할 수 있다고 생각했습니다. 그래서 몇 가지 조치를 취했다 내 ImageSelection 활동이 추가 :
@Override
public void onPause()
{
d.dismiss();
super.onPause();
}
는하지만 이건 내 문제가 해결되지 않은 : 내 질문 c를 그러므로 무슨 일이 내가 잘못 일을 오전과 어떻게 오류를 수정합니까? 목표는 두 개의 다른 활동에서 동일한 대화 (내가 만든 별도의 클래스에서)를 얻는 것입니다. 미리 감사드립니다.
편집 : 이제 "d.dismiss();"가 추가되었습니다. Hany Elsioufy가 제안한대로 대화 상자 클래스에 전달했지만 문제가 해결되지 않았습니다.
오류 메시지가'PhoneWindow $ DecorView'을 언급,하지만 난 둘 다'PhoneWindow'도'표시되지 않습니다 귀하의 코드에서 DecorView'. –
나는 내 코드에서 아무 것도 관련이 없기 때문에 솔직하게 그것이 무엇을 의미하는지 모른다. 그러나 나는 그것을봤을 때 대화를 기각하지 않을 가능성이 있음을 발견했습니다. – Nifty