Android Error: Unable to add window — token null is not for an application

烈酒焚心 提交于 2019-12-09 07:38:09

问题


Hy. I tried to create an alert dialog but when I run my application, is throw an exception

09-26 12:43:21.949: E/AndroidRuntime(14618): FATAL EXCEPTION: main
09-26 12:43:21.949: E/AndroidRuntime(14618): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:687)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.app.Dialog.show(Dialog.java:278)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at com.example.stampp.UI.Rewards.DeleteRewardsFragment$1.onItemClick(DeleteRewardsFragment.java:80)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.widget.AbsListView.performItemClick(AbsListView.java:1394)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3024)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.widget.AbsListView.onTouchEvent(AbsListView.java:3846)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.View.dispatchTouchEvent(View.java:5629)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1964)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1725)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2062)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1400)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.app.Activity.dispatchTouchEvent(Activity.java:2369)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2010)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.View.dispatchPointerEvent(View.java:5809)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3130)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2658)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:1015)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2667)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.os.Looper.loop(Looper.java:137)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at android.app.ActivityThread.main(ActivityThread.java:4517)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at java.lang.reflect.Method.invokeNative(Native Method)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at java.lang.reflect.Method.invoke(Method.java:511)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-26 12:43:21.949: E/AndroidRuntime(14618):    at dalvik.system.NativeStart.main(Native Method)

This is my code:

public class DeleteRewardsFragment extends Fragment{

private Context context;

private FragmentsActivity activity;

private ListView listView;
private View myFragmentView;
private DeleteRewardsAdapter adapter;
private String[] values;

@SuppressLint("ResourceAsColor")
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    myFragmentView = inflater.inflate(R.layout.listview, container, false);
    listView = (ListView) myFragmentView.findViewById(android.R.id.list);


    activity = (FragmentsActivity) getActivity();
    context = activity.getApplicationContext();

    values = new String[4];

    adapter = new DeleteRewardsAdapter(context, values);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(deleteEvent);

    return myFragmentView;

}

private OnItemClickListener deleteEvent = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        AlertDialog.Builder alertDialog  = new AlertDialog.Builder(context);
        alertDialog .setTitle("Jetzt löschen?");
        alertDialog .setMessage("Die in deisem Store gesammelten stampss gehen debei verloren!")
                .setCancelable(false)
                .setPositiveButton("Abbrechen", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        values = new String[values.length-1];
                        adapter = new DeleteRewardsAdapter(context, values);
                        listView.setAdapter(adapter);
                    }
                })
                .setNegativeButton("Löschen", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        AlertDialog dialog = alertDialog.create();
        dialog.show();

    }
};

}

Can someone to help me?


回答1:


To add AlertDialog to your Activity or Fragment you have to use your Activity's instance, not your application's which you are doing in your code. Create your AlertDialog like this :

AlertDialog.Builder alertDialog  = new AlertDialog.Builder(getActivity());

And that should do the trick for you! : )




回答2:


In my case I was trying to create my dialog like this:

new Dialog(getApplicationContext());

So I had to change for:

new Dialog(MyActivity.this);

And it started working.




回答3:


Below are the things you can do to get it over from issue.

  1. Instead of using

      context 
    you should use
      ActivityName.this 
  2. if you are using getApplicationContext(), then use below flag before using getApplicationContext ()

      dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)  

Add the following permission to your manifest:

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



回答4:


Send in activity, rather than context.

AlertDialog.Builder alertDialog  = new AlertDialog.Builder(activity);



回答5:


I changed:

ProgressDialog(getApplicationContext());

to:

ProgressDialog(MainActivity.this);

,




回答6:


I changed getApplicationContext() to

activity.this

it will work




回答7:


Change from getApplicationContext() or getApplication() to ActivityName.this

For more information, look at this Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context



来源:https://stackoverflow.com/questions/19024940/android-error-unable-to-add-window-token-null-is-not-for-an-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!