27

海。我试图创建一个警报对话框,但是当我运行我的应用程序时,抛出异常

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

    }
};

}

有人可以帮助我吗?

4

10 回答 10

54

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

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

这应该对你有用!:)

于 2013-09-26T09:52:23.777 回答
14

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

new Dialog(getApplicationContext());

所以我不得不改变:

new Dialog(MyActivity.this);

它开始工作了。

于 2015-10-27T03:24:48.087 回答
7

以下是您可以采取的措施来解决问题。

  1. 而不是使用

      context 
    你应该使用
      ActivityName.this 

  2. 如果您正在使用 getApplicationContext(),则在使用 getApplicationContext() 之前使用下面的标志

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

将以下权限添加到您的清单中:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 
于 2017-05-12T11:06:07.130 回答
4

我变了:

ProgressDialog(getApplicationContext());

至:

ProgressDialog(MainActivity.this);

,

于 2016-09-23T14:31:20.143 回答
3

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

AlertDialog.Builder alertDialog  = new AlertDialog.Builder(activity);
于 2013-09-26T10:04:01.377 回答
1

getApplicationContext() 改为

activity.this

它会起作用的

于 2017-11-08T10:59:27.217 回答
0

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

有关更多信息,请查看此对话框抛出“无法添加窗口 - 令牌 null 不适用于应用程序”,其中 getApplication() 作为上下文

于 2019-10-21T14:11:03.423 回答
0

由于不适当的上下文:

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

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

于 2019-12-31T11:01:10.210 回答
0

奋斗了好久!

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

mWindowParams.type = LayoutParams.TYPE_TOAST;

TYPE_TOAST:此常量在 API 级别 26 中已弃用。对于非系统应用程序。请改用 TYPE_APPLICATION_OVERLAY。

API 26 上的 SYSTEM_ALERT_WINDOW PERMISSION 未按预期工作。窗口类型 2002 的权限被拒绝

于 2020-01-11T12:48:49.307 回答
0

在清单上添加权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
于 2020-12-29T03:38:51.997 回答