티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
Android 1.6: "Android.view.WindowManager$BadTokenException 수정하기
문제 내용
I'm trying to open a dialog window, but every time I try to open it it throws this exception:
다이얼로그를 열려고 하지만 열려고 할 때마다 다음과 같은 예외가 발생합니다.
Uncaught handler: thread main exiting due to uncaught exception
android.view.WindowManager$BadTokenException:
Unable to add window -- token null is not for an application
at android.view.ViewRoot.setView(ViewRoot.java:460)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:238)
at android.app.Activity.showDialog(Activity.java:2413)
I'm creating it by calling showDialog
with the display's id. The onCreateDialog
handler logs fine and I can step through it without an issue, but I've attached it since it seems like I'm missing something:
디스플레이 ID로 showDialog를 호출하여 생성합니다. onCreateDialog 핸들러는 로그가 정상적으로 기록되고 문제 없이 진행할 수 있지만 뭔가 누락된 것 같아 첨부합니다.
@Override
public Dialog onCreateDialog(int id)
{
Dialog dialog;
Context appContext = this.getApplicationContext();
switch(id)
{
case RENAME_DIALOG_ID:
Log.i("Edit", "Creating rename dialog...");
dialog = new Dialog(appContext);
dialog.setContentView(R.layout.rename);
dialog.setTitle("Rename " + noteName);
break;
default:
dialog = null;
break;
}
return dialog;
}
Is there something missing from this? Some questions have talked about having this problem when creating a dialog from onCreate
, which happens because the activity isn't created yet, but this is coming from a call from a menu object, and the appContext
variable seems like it is correctly populated in the debugger.
여기서 뭔가 빠진 게 있나요? 일부 질문에서는 onCreate에서 대화 상자를 만들 때 이 문제가 발생하는 것에 대해 설명했습니다. 이 문제는 액티비티가 아직 만들어지지 않았기 때문에 발생하지만 메뉴 개체의 호출에서 발생하며 appContext 변수가 디버거에 올바르게 채워져 있는 것처럼 보입니다.
높은 점수를 받은 Solution
Instead of : Context appContext = this.getApplicationContext();
you should use a pointer to the activity you're in (probably this
).
Context appContext = this.getApplicationContext(); 대신 현재 수행 중인 액티비티에 대한 'this'를 사용해야 합니다.
I got bitten by this today too, the annoying part is the getApplicationContext()
is verbatim from developer.android.com :(
나는 오늘도 이것에 물렸습니다. 성가신 부분은 getApplicationContext()가 developer.android.com에서 그대로입니다 :(
가장 최근 달린 Solution
Another solution is to set the window type to a system dialog:
또 다른 해결책은 윈도우 타입을 시스템 대화 상자로 설정하는 것입니다.
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
This requires the SYSTEM_ALERT_WINDOW
permission:
이 작업을 수행하려면 SYSTEM_ALERT_WINDOW 권한이 필요합니다:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
As the docs say:
doc에서는 말합니다:
Very few applications should use this permission; these windows are intended for system-level interaction with the user.
이 권한을 사용해야 하는 애플리케이션은 거의 없습니다. 이러한 윈도우는 사용자와의 시스템 수준 상호 작용을 위한 것입니다.
This is a solution you should only use if you require a dialog that's not attached to an activity.
이 솔루션은 액티비티에 연결되지 않은 대화 상자가 필요한 경우에만 사용해야 합니다.
출처 : https://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window
'개발 > 안드로이드' 카테고리의 다른 글
Android WebView 투명 배경 적용하기 (0) | 2022.12.07 |
---|---|
Android ADB로 인텐트 보내기 (0) | 2022.12.07 |
Android 웹뷰 로딩 속도 문제 (0) | 2022.12.06 |
AsyncTask Android 예제 (0) | 2022.12.06 |
서비스에서 컨텍스트 가져오기 (0) | 2022.12.06 |