2013-04-14 5 views
1
경고 대화를 할 때 나는 어려움을 겪고있어

을 만들려고, 그것을위한 코드는 여기에 있습니다 : 내가 이것을 실행할 때마다프로그램 충돌이 발생하는 경우에 AlertDialog

public void setp2(View v){ 
     p2 = (TextView)findViewById(R.id.p2); 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext()); 
     alertDialogBuilder.setTitle("Please enter player 2 name"); 
     alertDialogBuilder.setMessage("MESSAGE"); 
     alertDialogBuilder.setCancelable(false); 
     alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int id) { 
       // TODO Auto-generated method stub 
       dialog.cancel(); 
      } 
     }); 
     AlertDialog alert = alertDialogBuilder.create(); 
     alert.show(); 

} 

IT는 로그 캣 보이는 충돌과 같은 :

04-14 06:09:36.282: E/Trace(25945): error opening trace file: No such file or directory (2) 
04-14 06:09:37.822: E/AndroidRuntime(25945): FATAL EXCEPTION: main 
04-14 06:09:37.822: E/AndroidRuntime(25945): java.lang.IllegalStateException: Could not execute method of the activity 
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.View$1.onClick(View.java:3595) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.View.performClick(View.java:4088) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.View$PerformClick.run(View.java:16984) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.os.Handler.handleCallback(Handler.java:615) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.os.Looper.loop(Looper.java:137) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.app.ActivityThread.main(ActivityThread.java:4745) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at java.lang.reflect.Method.invokeNative(Native Method) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at java.lang.reflect.Method.invoke(Method.java:511) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at dalvik.system.NativeStart.main(Native Method) 
04-14 06:09:37.822: E/AndroidRuntime(25945): Caused by: java.lang.reflect.InvocationTargetException 
04-14 06:09:37.822: E/AndroidRuntime(25945): at java.lang.reflect.Method.invokeNative(Native Method) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at java.lang.reflect.Method.invoke(Method.java:511) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.View$1.onClick(View.java:3590) 
04-14 06:09:37.822: E/AndroidRuntime(25945): ... 11 more 
04-14 06:09:37.822: E/AndroidRuntime(25945): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.ViewRootImpl.setView(ViewRootImpl.java:589) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.app.Dialog.show(Dialog.java:277) 
04-14 06:09:37.822: E/AndroidRuntime(25945): at uk.co.mikecoombes.oxo.MainActivity.setp2(MainActivity.java:46) 
04-14 06:09:37.822: E/AndroidRuntime(25945): ... 14 more 

여러 번 살펴 봤는데 어디가 잘못 됐는지 찾을 수 없는데, 누군가 도와 줄 수 있는지 궁금해하고 있었습니까?

+0

AllComediansActivity.java 파일에있는 내용은 무엇입니까? – stinepike

+0

스택 트레이스에서'setp2()'이 보이지 않습니다. 문제는 AsynTask의'doInBackground()'에 있습니다.이 메소드를 게시하고 93 행을 표시하십시오. – Sam

+0

내 Logcat이 저장되지 않은 것 같지 않습니다. 이전에 저장 한 로그를 지금 정렬해야합니다. – Coombes

답변

4
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 

문맥 가끔, 나쁜 나타납니다 getApplicationContext() 반환 이상한 결과 ...이 변경 시도 :

new AlertDialog.Builder(getApplicationContext()); 

으로는 :

new AlertDialog.Builder(MainActivity.this); 

링크에 commonsware하여 답을 확인

When to call activity context OR application context?

+2

답변이 동일하므로 답변을 삭제했습니다. 귀하의 답변에 링크를 복사하여 게시했습니다. 링크가 도움이 될 것이라고 생각했습니다. – Raghunandan

+0

빙고 이것은 트릭을 했어! – Coombes

+0

감사합니다 RaghuNandan, 당신은 SO의 규칙을 존중했습니다 :) – AAnkit

관련 문제