IT

Android '창을 추가 할 수 없음-토큰 널이 애플리케이션 용이 아닙니다'예외

lottoking 2020. 6. 26. 07:48
반응형

Android '창을 추가 할 수 없음-토큰 널이 애플리케이션 용이 아닙니다'예외


대화 상자를 열려고 할 때 다음과 같은 Android 예외가 발생합니다. 누군가 무슨 일이 일어나고 있는지 이해 하고이 문제를 어떻게 해결할 수 있습니까?

android.view.WindowManager$BadTokenException: 
  Unable to add window -- token null is not for an application
    at android.view.ViewRoot.setView(ViewRoot.java:509)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    at android.app.Dialog.show(Dialog.java:241)

추측하고 있습니다-응용 프로그램 컨텍스트로 대화 상자를 만들려고합니까? 이 같은:

new Dialog(getApplicationContext());

이것은 잘못이다. 활동 컨텍스트를 사용해야합니다.

당신은 다음과 같이 시도해야합니다 :

new Dialog(YourActivity.this);

계속 사용할 수 getApplicationContext()있지만 사용하기 전에이 플래그를 추가해야합니다 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT). 오류가 표시되지 않습니다.

그리고 권한을 추가하는 것을 잊지 마십시오 :

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

내 경우에는 다음과 같이 대화 상자를 만들려고했습니다.

new Dialog(getApplicationContext());

그래서 나는 다음과 같이 바꿔야했습니다.

new Dialog(this);

그리고 그것은 나를 위해 잘 작동합니다.)


시도 getParent()의 인수 장소에서 context같은 new AlertDialog.Builder(getParent());작동합니다 희망이 나를 위해 일했다.


나는 추측하고있다-당신은 Dialog를 사용하여 만들려고하고있다.

 getApplicationContext()
 mContext which is passed by activity.

비 활동 클래스 대화 상자를 표시하는 경우 활동을 매개 변수로 전달해야합니다.

Activity activity=YourActivity.this;

이제는 잘 작동합니다.

문제가 있으면 알려주세요.


나는 문맥 필드에서 이것을 시도했다.

this.getActivity().getParent()

그리고 그것은 나를 위해 잘 작동합니다. 이것은 "Fragment"에서 확장 된 클래스에서 온 것입니다.

public class filtro extends Fragment{...

나는 같은 예외가 있습니다. 내가 이것을 해결하기 위해 대화 상자의 인스턴스를 매개 변수로 함수에 전달하고 컨텍스트 만 전달하는 대신 getContext ()를 사용하는 대신 사용하십시오. 이 솔루션은 내 문제를 해결하고 도움이되기를 바랍니다.


안녕하세요, 당신이 어댑터를 사용하는 경우 기회가있을 수 있습니다.
어댑터, getContext (), 컨텍스트 또는 활동에서 대화 상자를 사용했을 때 알아야 할 것은 언젠가 작동하지 않습니다.

여기 v.getRootView().getContext()에 v가 참조하는 뷰 객체 가있는 트릭 이 있습니다.
예 :


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(v.getRootView().getContext(), date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });  
If you are getting this problem because of alert dialog.
Refer [here][1] But it is same concept.


  [1]: https://stackoverflow.com/questions/6367771/displaying-alertdialog-inside-a-custom-listadapter-class

I got this exception, when I tried to open Progress Dialog under Cordova Plugin by using below two cases,

  1. new ProgressDialog(this.cordova.getActivity().getParent());

  2. new ProgressDialog(this.cordova.getActivity().getApplicationContext());

Later changed like this,

new ProgressDialog(this.cordova.getActivity());

Its working fine for me.


Use this and context not worked for me..but MyActivityName.this worked. Hope this helps anyone who need it.


I solved this error by add below user-permission in AndroidManifest.xml

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Also, Initialize Alert dialog with Activity Name:

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);

For More Details, visit==> How to create Alert Dialog in Android

참고URL : https://stackoverflow.com/questions/7933206/android-unable-to-add-window-token-null-is-not-for-an-application-exception

반응형