[英]Unable to add window (Android)
I've tried using the context from new AlertDialog.Builder(mContext)
with the context from adapter constructor, ie mContext
, with 我尝试将new AlertDialog.Builder(mContext)
中的上下文与适配器构造函数中的上下文(即mContext
)一起使用
My deleteComment() function: 我的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());
}
});
}
The exception: 例外:
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)
Here is a gist of my Adapter class if that helps: 如果有帮助,这是我的Adapter类的要点:
https://gist.github.com/santafebound/bf496bbfee2da81b60312207121853b0 https://gist.github.com/santafebound/bf496bbfee2da81b60312207121853b0
Make sure mContext is an Activity. 确保mContext是一个Activity。
Calling new AlertDialog.Builder(activity.getApplicationContext())
will throw this same error, but new AlertDialog.Builder(activity)
works fine when calling show()
. 调用new AlertDialog.Builder(activity.getApplicationContext())
会引发相同的错误,但是调用show()
时, new AlertDialog.Builder(activity)
可以正常工作。
Looks like AlertDialog is designed to use context from visible elements only, as an Activity, and not from an ApplicationContext, Service, etc. 看起来AlertDialog被设计为仅将可见元素的上下文用作活动,而不是ApplicationContext,Service等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.