- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
从我的主 activity
中,我需要调用一个内部类,并且在该类中的一个方法中,我需要显示 AlertDialog
。关闭后,按下 OK 按钮后,转到 Google Play 购买。
大多数情况下一切正常,但对于少数用户来说,它在 builder.show()
上崩溃,我可以看到 "android.view.WindowManager$BadTokenException:
无法从崩溃日志中添加窗口。请提出建议。
我的代码差不多是这样的:
public class classname1 extends Activity{
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.<view>);
//call the <className1> class to execute
}
private class classNamename2 extends AsyncTask<String, Void, String>{
protected String doInBackground(String... params) {}
protected void onPostExecute(String result){
if(page.contains("error"))
{
AlertDialog.Builder builder = new AlertDialog.Builder(classname1.this);
builder.setCancelable(true);
builder.setMessage("");
builder.setInverseBackgroundForced(true);
builder.setNeutralButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
dialog.dismiss();
if(!<condition>)
{
try
{
String pl = "";
mHelper.<flow>(<class>.this, SKU, RC_REQUEST,
<listener>, pl);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
});
builder.show();
}
}
}
}
我还在另一个警报中看到了错误,我没有转发到任何其他 activity
。很简单:
AlertDialog.Builder builder = new AlertDialog.Builder(classname1.this);
builder.setCancelable(true);
//if successful
builder.setMessage(" ");
builder.setInverseBackgroundForced(true);
builder.setNeutralButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
// dialog.dismiss();
}
});
builder.show();
}
最佳答案
android.view.WindowManager$BadTokenException: Unable to add window"
问题:
This exception occurs when the app is trying to notify the user from the background thread (AsyncTask) by opening a Dialog.
If you are trying to modify the UI from background thread (usually from onPostExecute() of AsyncTask) and if the activity enters finishing stage i.e.) explicitly calling finish(), user pressing home or back button or activity clean up made by Android then you get this error.
原因:
The reason for this exception is that, as the exception message says, the activity has finished but you are trying to display a dialog with a context of the finished activity. Since there is no window for the dialog to display the android runtime throws this exception.
解决方案:
Use
isFinishing()
method which is called by Android to check whether this activity is in the process of finishing: be it explicit finish() call or activity clean up made by Android. By using this method it is very easy to avoid opening dialog from background thread when activity is finishing.Also maintain a
weak reference
for the activity (and not a strong reference so that activity can be destroyed once not needed) and check if the activity is not finishing before performing any UI using this activity reference (i.e. showing a dialog).
例如。
private class chkSubscription extends AsyncTask<String, Void, String>{
private final WeakReference<login> loginActivityWeakRef;
public chkSubscription (login loginActivity) {
super();
this.loginActivityWeakRef= new WeakReference<login >(loginActivity)
}
protected String doInBackground(String... params) {
//web service call
}
protected void onPostExecute(String result) {
if(page.contains("error")) //when not subscribed
{
if (loginActivityWeakRef.get() != null && !loginActivityWeakRef.get().isFinishing()) {
AlertDialog.Builder builder = new AlertDialog.Builder(login.this);
builder.setCancelable(true);
builder.setMessage(sucObject);
builder.setInverseBackgroundForced(true);
builder.setNeutralButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
dialog.dismiss();
}
});
builder.show();
}
}
}
}
更新:
窗口标记:
As its name implies, a window token is a special type of Binder token that the window manager uses to uniquely identify a window in the system. Window tokens are important for security because they make it impossible for malicious applications to draw on top of the windows of other applications. The window manager protects against this by requiring applications to pass their application's window token as part of each request to add or remove a window. If the tokens don't match, the window manager rejects the request and throws a BadTokenException. Without window tokens, this necessary identification step wouldn't be possible and the window manager wouldn't be able to protect itself from malicious applications.
真实场景:
When an application starts up for the first time, the ActivityManagerService creates a special kind of window token called an application window token, which uniquely identifies the application's top-level container window. The activity manager gives this token to both the application and the window manager, and the application sends the token to the window manager each time it wants to add a new window to the screen. This ensures secure interaction between the application and the window manager (by making it impossible to add windows on top of other applications), and also makes it easy for the activity manager to make direct requests to the window manager.
关于android - "android.view.WindowManager$BadTokenException: Unable to add window"上 buider.show(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18662239/
我有一个建立异步连接的 Activity ,如下所示: new Thread(new Runnable() { public void run() {
我在 Android 应用程序上有一个 Activity ,该 Activity 启动与我的服务器的同步进程。此过程消耗大量内存和处理,需要一些时间才能完成。 该过程完成后,将向用户显示一个 Aler
自从转移到 androidx 后开始在 Android 7.1 上出现 BadTokenException。它应该是 7.1 中 Toast 的一个已知错误 link但我不太确定,因为它是在我转向 a
我正在尝试在 API 25 中运行我的应用程序,但是当我点击按钮时出现此错误: E/ACRA: ACRA caught a BadTokenException for com.safa.visit.t
当我运行我的代码时,我得到这个错误: : E/InputEventReceiver(1363): Exception dispatching input event. : E/MessageQ
我正在尝试将进度对话框放在 ListView 的 Click 事件上,如下面的代码所述,但出现错误“WindowManager$BadTokenException:无法添加窗口—— token and
我已经在 Android 市场上发布了一个应用程序。我已经在 HTC 和三星上测试过,它工作正常。但是今天我的应用程序出现了如下错误。 android.view.WindowManager$BadTo
我正在读取 GPS 信息并每 10 秒将其发送到网络。 我正在做的是使用运行我的服务类 GPSTracker 的处理程序 public void startGpsService() {
我已经针对同一问题查找了几个帖子,但似乎无法解决我的问题。我在整个应用程序中都使用了微调器,它们运行良好。当我尝试在弹出窗口中使用微调器时,选择它时出现错误。弹出窗口用于添加引用,我已经声明了一个全局
我正在开发一个包含多个 Activity 和服务的 Android 应用程序。一些 Activity 是在我导入到我的项目中的第 3 方库中定义的,问题是在某些设备上(特别是在三星 Galaxy Ta
我正在开发用于背景视频录制 的应用程序,这就是我使用 WindowManager 的原因,但它对我不起作用。出现以下错误: 08-23 15:38:21.021: E/AndroidRuntime(4
如果我直接在oCreate()中添加showPopupWindow();,会报错: android.view.WindowManager$BadTokenException: Unable to ad
我正在使用 Tabwidget,Tab 的实现方式与 tutorial 中所示的方式相同 “MainActivity -> Activity1 -> Activity2(给定图像)” 现在点击微调器给
我创建了一个显示按钮列表的 PopupWindow。最初,对于我列表中的每个项目,我实现了一个 OnLongClickListener 以在 View 持有者中显示一个 Toast 消息: priva
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@4
我试图在单击按钮后显示 DatePickerDialog,但是一旦单击按钮,它就会停止应用程序并引发以下异常: 05-06 08:46:00.688 18786-18786/carloscoronad
首先,我很清楚发生此错误是因为我试图通过不是Activity 的Context 调用窗口/对话框。 但是没有任何解决方案。我的要求是;我在普通 JAVA 类的方法中有一个带有自定义样式表的 Dialo
当我的应用程序正在运行时,出现此错误: android.view.WindowManager$BadTokenException: Unable to add window -- token null
我正在尝试创建一个覆盖窗口,但是当我尝试将 View 添加到 WindowManager 时,出现异常。我添加了“SYSTEM_ALERT_WINDOW”权限,并在应用信息中启用了“在其他应用上绘制”
我进行了大量搜索以找到答案,但仍然不知道我做错了什么。我只是尝试将 AutoCompleteTextView 与动态数组适配器一起使用。但它对我不起作用:( 我的 OnCreate 方法如下所示: i
我是一名优秀的程序员,十分优秀!