在 onCancelled throws 和 Exception 中显示 AlertDialog
Showing AlertDialog in onCancelled throws and Exception
我正在开发一个 Android 应用程序,我正在使用 AsyncTask 连接到服务器、发送和检索一些值等。
在连接到服务器之前,我检查 phone 是否已连接到互联网,并在 doInBackground()
中进行一些验证检查。如果这些检查中的任何一个失败,我将取消任务并设置一个取消消息字符串,稍后将使用它向用户提供正确的消息。
现在,当我想在 onCancelled 方法中显示 AlertDialog 时,问题就出现了。我尝试展示 Toast,只是为了确保我可以到达 activity,它起作用了。
在网上搜索了几个小时并尝试自己解决问题后,我没有找到解决方案。
注意: 我正在扩展的 class 的构造函数中传递当前应用程序上下文。
@Override
protected void onCancelled(){
String cancellationMessage = "";
// Some stuff goes in here to set the proper value for cancellationMessage.
AlertDialog.Builder builder = new AlertDialog.Builder(currentActivityContext);
builder.setTitle("Oops, something went wrong!")
.setMessage(cancellationMessage)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create().show();
}
看起来调用 builder.show();
会抛出该异常。
为什么 onCancelled 在 UI 线程上运行时会出现这个问题?任何的想法?
顺便说一句,当我尝试显示警告对话框时,我在 logcat 选项卡中收到此异常:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
好的,伙计们,我找到了解决方案。
我在 class 的构造函数中将 this
作为 activity 上下文传递,而不是 getApplicationContext()
。它现在就像一个魅力,嘿!
旧代码:
public void signMeIn(View v){
String email = (String) ((EditText)findViewById(R.id.signInEmailText)).getText().toString();
String password = (String) ((EditText)findViewById(R.id.signInPasswordText)).getText().toString();
UserCredentialsAsync uca = new UserCredentialsAsync(getApplicationContext());
uca.signIn(email, password);
uca.execute();
}
以及新代码:
public void signMeIn(View v){
String email = (String) ((EditText)findViewById(R.id.signInEmailText)).getText().toString();
String password = (String) ((EditText)findViewById(R.id.signInPasswordText)).getText().toString();
UserCredentialsAsync uca = new UserCredentialsAsync(this);
uca.signIn(email, password);
uca.execute();
}
我正在开发一个 Android 应用程序,我正在使用 AsyncTask 连接到服务器、发送和检索一些值等。
在连接到服务器之前,我检查 phone 是否已连接到互联网,并在 doInBackground()
中进行一些验证检查。如果这些检查中的任何一个失败,我将取消任务并设置一个取消消息字符串,稍后将使用它向用户提供正确的消息。
现在,当我想在 onCancelled 方法中显示 AlertDialog 时,问题就出现了。我尝试展示 Toast,只是为了确保我可以到达 activity,它起作用了。
在网上搜索了几个小时并尝试自己解决问题后,我没有找到解决方案。
注意: 我正在扩展的 class 的构造函数中传递当前应用程序上下文。
@Override
protected void onCancelled(){
String cancellationMessage = "";
// Some stuff goes in here to set the proper value for cancellationMessage.
AlertDialog.Builder builder = new AlertDialog.Builder(currentActivityContext);
builder.setTitle("Oops, something went wrong!")
.setMessage(cancellationMessage)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create().show();
}
看起来调用 builder.show();
会抛出该异常。
为什么 onCancelled 在 UI 线程上运行时会出现这个问题?任何的想法?
顺便说一句,当我尝试显示警告对话框时,我在 logcat 选项卡中收到此异常:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
好的,伙计们,我找到了解决方案。
我在 class 的构造函数中将 this
作为 activity 上下文传递,而不是 getApplicationContext()
。它现在就像一个魅力,嘿!
旧代码:
public void signMeIn(View v){
String email = (String) ((EditText)findViewById(R.id.signInEmailText)).getText().toString();
String password = (String) ((EditText)findViewById(R.id.signInPasswordText)).getText().toString();
UserCredentialsAsync uca = new UserCredentialsAsync(getApplicationContext());
uca.signIn(email, password);
uca.execute();
}
以及新代码:
public void signMeIn(View v){
String email = (String) ((EditText)findViewById(R.id.signInEmailText)).getText().toString();
String password = (String) ((EditText)findViewById(R.id.signInPasswordText)).getText().toString();
UserCredentialsAsync uca = new UserCredentialsAsync(this);
uca.signIn(email, password);
uca.execute();
}