Android ‘창을 추가 할 수 없음-토큰 널이 애플리케이션 용이 아닙니다’예외 add window — token null is not

대화 상자를 열려고 할 때 다음과 같은 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());작동합니다 희망이 나를 위해 일했다.


답변

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

 getApplicationContext()
 mContext which is passed by activity.

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

Activity activity=YourActivity.this;

이제는 잘 작동합니다.

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


답변

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

this.getActivity().getParent()

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

public class filtro extends Fragment{...

답변

안녕하세요, 당신이 어댑터를 사용하는 경우 기회가있을 수 있습니다.
어댑터, 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