안드로이드 개발 질문/답변
(글 수 45,052)
안녕하세요~ 공부한지 얼마 되지 않은 초보 개발자입니다 +_+
Dialog 를 직접 만들어서 띄우려고 하는데 show() 하는 부분에서 에러가 나네요 ㅠ
아래는 관련 로그입니다.
07-25 16:28:36.936: ERROR/Dialog(13762): [show] Exception occured!!
07-25 16:28:36.936: ERROR/Dialog(13762): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-25 16:28:36.936: ERROR/Dialog(13762): at android.view.ViewRoot.setView(ViewRoot.java:472)
07-25 16:28:36.936: ERROR/Dialog(13762): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
07-25 16:28:36.936: ERROR/Dialog(13762): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-25 16:28:36.936: ERROR/Dialog(13762): at android.app.Dialog.show(Dialog.java:240)
07-25 16:28:36.936: ERROR/Dialog(13762): at pe.opus37.memorizer.Main$1.onClick(Main.java:96)
07-25 16:28:36.936: ERROR/Dialog(13762): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
07-25 16:28:36.936: ERROR/Dialog(13762): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 16:28:36.936: ERROR/Dialog(13762): at android.os.Looper.loop(Looper.java:123)
07-25 16:28:36.936: ERROR/Dialog(13762): at android.app.ActivityThread.main(ActivityThread.java:4364)
07-25 16:28:36.936: ERROR/Dialog(13762): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 16:28:36.936: ERROR/Dialog(13762): at java.lang.reflect.Method.invoke(Method.java:521)
07-25 16:28:36.936: ERROR/Dialog(13762): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
07-25 16:28:36.936: ERROR/Dialog(13762): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
07-25 16:28:36.936: ERROR/Dialog(13762): at dalvik.system.NativeStart.main(Native Method)
관련된 소스 코드는 아래와 같습니다.
46번째 라인에서 show() 를 호출할 때 예외가 발생했습니다.
Android API 1.5 를 쓰고 있고요,
관련해서 검색해 봤는데 getApplicationContext() 가 null 일 수 있다기에 널 체크를 해봤지만 널은 아니었습니다.
좋은 답변 주시면 감사하겠습니다!! 몇 시간째 해결 방법을 찾고 있는데 잘 안되네요 ㅠㅠ
공지사항에 있는 질문법 글은 읽었습니다~ :)
Dialog 를 직접 만들어서 띄우려고 하는데 show() 하는 부분에서 에러가 나네요 ㅠ
아래는 관련 로그입니다.
07-25 16:28:36.936: ERROR/Dialog(13762): [show] Exception occured!!
07-25 16:28:36.936: ERROR/Dialog(13762): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-25 16:28:36.936: ERROR/Dialog(13762): at android.view.ViewRoot.setView(ViewRoot.java:472)
07-25 16:28:36.936: ERROR/Dialog(13762): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
07-25 16:28:36.936: ERROR/Dialog(13762): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-25 16:28:36.936: ERROR/Dialog(13762): at android.app.Dialog.show(Dialog.java:240)
07-25 16:28:36.936: ERROR/Dialog(13762): at pe.opus37.memorizer.Main$1.onClick(Main.java:96)
07-25 16:28:36.936: ERROR/Dialog(13762): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
07-25 16:28:36.936: ERROR/Dialog(13762): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 16:28:36.936: ERROR/Dialog(13762): at android.os.Looper.loop(Looper.java:123)
07-25 16:28:36.936: ERROR/Dialog(13762): at android.app.ActivityThread.main(ActivityThread.java:4364)
07-25 16:28:36.936: ERROR/Dialog(13762): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 16:28:36.936: ERROR/Dialog(13762): at java.lang.reflect.Method.invoke(Method.java:521)
07-25 16:28:36.936: ERROR/Dialog(13762): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
07-25 16:28:36.936: ERROR/Dialog(13762): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
07-25 16:28:36.936: ERROR/Dialog(13762): at dalvik.system.NativeStart.main(Native Method)
관련된 소스 코드는 아래와 같습니다.
@Override public void onClick(DialogInterface dialog, int which) { Dialog newMemorizerDialog = new Dialog(getApplicationContext()); newMemorizerDialog.setContentView(R.layout.new_memorizer_dialog); newMemorizerDialog.setTitle(getApplicationContext().getString(R.string.new_memorizer)); Button confirmButton = (Button)newMemorizerDialog.findViewById(R.id.new_memorizer_confirm); confirmButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Dialog parent = (Dialog)v.getParent(); EditText nameEditText = (EditText)parent.findViewById(R.id.new_memozier_edittext); String name = nameEditText.getText().toString(); if (name.length() > 0) { // create a memorizer. Main rootParent = (Main)v.getParent().getParent(); rootParent.dbHelper.createMemorizer(name); // toast and close the dialog. Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.memorizer_created), Toast.LENGTH_SHORT); } else { Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.memorizer_name_required), Toast.LENGTH_SHORT); } parent.dismiss(); } }); Button cancelButton = (Button)newMemorizerDialog.findViewById(R.id.new_memorizer_cancel); cancelButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Dialog parent = (Dialog)v.getParent(); parent.dismiss(); } }); newMemorizerDialog.show(); }
46번째 라인에서 show() 를 호출할 때 예외가 발생했습니다.
Android API 1.5 를 쓰고 있고요,
관련해서 검색해 봤는데 getApplicationContext() 가 null 일 수 있다기에 널 체크를 해봤지만 널은 아니었습니다.
좋은 답변 주시면 감사하겠습니다!! 몇 시간째 해결 방법을 찾고 있는데 잘 안되네요 ㅠㅠ
공지사항에 있는 질문법 글은 읽었습니다~ :)
getApplicationContext() 사용하지 마시고, 액티비티명.this를 한번 넣어보세요.