Android错误:无法添加窗口 - 令牌null不适用于应用程序

时间:2013-09-26 09:50:21

标签: android android-alertdialog android-dialog

HY。我尝试创建一个警告对话框,但是当我运行我的应用程序时,会抛出异常

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)

这是我的代码:

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();

    }
};

}

有人可以帮助我吗?

10 个答案:

答案 0 :(得分:49)

要将AlertDialog添加到ActivityFragment,您必须使用Activity的实例,而不是您在代码中使用的应用程序。像这样创建AlertDialog

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

这应该为你做的伎俩! :)

答案 1 :(得分:10)

就我而言,我试图像这样创建对话框:

new Dialog(getApplicationContext());

所以我不得不改变:

new Dialog(MyActivity.this);

它开始工作了。

答案 2 :(得分:5)

以下是您可以从问题中解决问题的方法。

  1. 您应该使用

      context 

  2. 而不是

      ActivityName.this 
  3. 如果你使用的是getApplicationContext(),那么在使用getApplicationContext()之前使用下面的标志

      dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)  
  4. 将以下权限添加到您的清单:

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

答案 3 :(得分:3)

发送活动,而不是上下文。

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

答案 4 :(得分:3)

我改变了:

ProgressDialog(getApplicationContext());

为:

ProgressDialog(MainActivity.this);

答案 5 :(得分:1)

我改变了getApplicationContext()  到

activity.this

它会起作用

答案 6 :(得分:0)

getApplicationContext()getApplication()更改为ActivityName.this

有关更多信息,请查看此Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

答案 7 :(得分:0)

由于上下文不适当:

需要确保您提供的上下文是活动上下文。

  

AlertDialog.Builder alertDialog = new AlertDialog.Builder 在此处提供活动上下文 );

答案 8 :(得分:0)

长时间奋斗!

如果您在Android SDK 26+中有此问题,则是由于以下错误设置引起的:

mWindowParams.type = LayoutParams.TYPE_TOAST;

TYPE_TOAST: 在API级别26中不赞成使用此常数。 非系统应用。请改用TYPE_APPLICATION_OVERLAY。

SYSTEM_ALERT_WINDOW PERMISSION on API 26 not working as expected. Permission denied for window type 2002

答案 9 :(得分:0)

在清单上添加权限

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