2014-12-07 6 views
-1

사용자가 이미지를 선택하고 퍼즐이 만들어지는 게임을 만들고 있습니다. 첫 번째 화면에서는 사용자가 이미지를 선택하고 클릭 할 때 나타나는 대화 상자가 표시되어 사용자가 어려움을 선택할 수 있습니다. 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가 제안한대로 대화 상자 클래스에 전달했지만 문제가 해결되지 않았습니다.

+0

오류 메시지가'PhoneWindow $ DecorView'을 언급,하지만 난 둘 다'PhoneWindow'도'표시되지 않습니다 귀하의 코드에서 DecorView'. –

+0

나는 내 코드에서 아무 것도 관련이 없기 때문에 솔직하게 그것이 무엇을 의미하는지 모른다. 그러나 나는 그것을봤을 때 대화를 기각하지 않을 가능성이 있음을 발견했습니다. – Nifty

답변

0

해결했습니다.

내가 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

AlertDialog d를 정의하십시오.

d.dismiss(); 
+0

더 자세히 설명해 주시겠습니까? 이것을 넣으면 GameActivity라고 가정 해 보겠습니다. null 포인터 참조에 대한 오류가 발생합니다. 존재하지 않는 무언가에 대해 'dismiss'를 호출하기 때문입니다. – Nifty

+0

다른 활동으로 이동하기 전에 대화 상자를 닫고 인스턴스 변수에 AlertDialog d를 선언하면 AlertDialog d = builder.create();가 표시됩니다. 잘 d = builder.create();로 바꿉니다. d.dismiss()를 호출하십시오. beforeActivity() Methods –

+0

전에 보았습니다. 문제는 대화 상자가 별도의 클래스에 있다는 것입니다. startActivity도 그 클래스에 있는데, 이는 내가 d를 호출하면 의미한다.해당 클래스 내에서 dismiss()를 호출하면 대화 상자가 Activity에서 호출 될 때 표시되지 않습니다. 어떻게 수정해야합니까? – Nifty

1

@Override 공공 부울 onOptionsItemSelected이 (MenuItem의 항목) {

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

} 위의 코드에서

: 인스턴스 변수로하고 어떤 startActivity를하거나 텐트 쓰기 전에 break 문이 아닙니다. 스위치 케이스를 사용할 때는 break 문을 사용하십시오.

+0

오! 그런 다음 반환 할 onOptionsItemSelected에 무엇을 넣을까요? 나는 그것이 부울임을 이해하기 때문에. – Nifty

+0

스위치 끝 반환 다음에이 줄을 추가하십시오. super.onOptionsItemSelected (item) – kanivel

+0

신경 쓰지 마세요, 해결책을 찾았습니다! 저를 올바른 길로 인도 해 주셔서 감사합니다. – Nifty