无法添加窗口(Android)

时间:2016-09-25 17:01:57

标签: java android alertdialog android-alertdialog android-windowmanager

我尝试使用new AlertDialog.Builder(mContext)中的上下文和适配器构造函数的上下文,即mContext

我的deleteComment()函数:

private void deleteComment(int position) {
        String currentUserObjectId = ParseUser.getCurrentUser().getObjectId();
        ParseQuery<ParseObject> query = new ParseQuery<>("Yeet");
        query.whereEqualTo(ParseConstants.KEY_OBJECT_ID, mYeets.get(position).getObjectId());
        query.whereContains(ParseConstants.KEY_SENDER_ID, currentUserObjectId);
        query.findInBackground((yeet, e) -> {
            if (e == null) {

                for (ParseObject yeetObject : yeet) {

                    if (yeetObject.getParseObject(ParseConstants.KEY_SENDER_AUTHOR_POINTER).getObjectId().equals((ParseUser.getCurrentUser().getObjectId()))) {

                        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
                        dialogBuilder.setTitle("Delete");
                        dialogBuilder.setMessage("Do you want to delete this Yeet?");
                        dialogBuilder.setPositiveButton("Yes", (dialog, which) -> {

                            // Iterate over all messages and delete them
                            for (ParseObject delete : yeet) {

                                delete.deleteInBackground();

                                /*this.adapter.remove(mYeets.get(position));*/
                                this.adapter.notifyDataSetChanged();

                                Toast.makeText(mContext, "Yeet deleted", Toast.LENGTH_SHORT).show();
                            }

                        });
                        dialogBuilder.setNegativeButton("No", (dialog, which) -> {
                        });
                        AlertDialog alertDialog = dialogBuilder.create();
                        alertDialog.show();
                    }
                }

            } else {
                Log.e("Error", e.getMessage());
            }
        });
    }

例外:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
    at android.app.Dialog.show(Dialog.java:319)
    at com.yitter.profile.UserProfileAdapter.lambda$deleteComment$115(UserProfileAdapter.java:286)
    at com.yitter.profile.UserProfileAdapter.access$lambda$2(UserProfileAdapter.java:0)
    at com.yitter.profile.UserProfileAdapter$$Lambda$3.done(Unknown Source)
    at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

以下是我的Adapter类的要点,如果有帮助:

https://gist.github.com/santafebound/bf496bbfee2da81b60312207121853b0

1 个答案:

答案 0 :(得分:0)

确保mContext是一个活动。

调用new AlertDialog.Builder(activity.getApplicationContext())会抛出同样的错误,但调用new AlertDialog.Builder(activity)show()工作正常。

看起来AlertDialog被设计为仅使用来自可见元素的上下文,作为Activity,而不是来自ApplicationContext,Service等。