想要在Android上为错误生成一条弹出消息吗
这是我想要添加弹出消息的方法。如果结果集包含“404”,我想显示一条消息。我不能在这里使用Toast,因为这不是在任何活动上下文中编写的想要在Android上为错误生成一条弹出消息吗,android,error-handling,popup,toast,Android,Error Handling,Popup,Toast,这是我想要添加弹出消息的方法。如果结果集包含“404”,我想显示一条消息。我不能在这里使用Toast,因为这不是在任何活动上下文中编写的 protected void onPostExecute(String result) { // To make a popup notification when it is a Not Found Location if(result.contains("404")){ return; } super.on
protected void onPostExecute(String result) {
// To make a popup notification when it is a Not Found Location
if(result.contains("404")){
return;
}
super.onPostExecute(result);
if (baseFragment != null) {
if (isMainTask) {
baseFragment.onTaskFinished(result);
} else {
baseFragment.onSubTaskFinished(result);
}
}
}
您可以使用
baseFragment.getActivity()获取活动上下文使用baseFragment.getActivity().getApplicationContext()
编码>或应用程序上下文,并使用应用程序上下文显示Toast,或者也可以使用活动上下文显示警报对话框
注意:要显示对话框,必须使用活动上下文,否则它将引发异常。如果要显示对话框,则需要有对上下文的引用。在调用AsyncTask的execute方法之前,可以通过类构造函数或setter方法向AsyncTask传递对上下文的引用。要创建任何类型的ErrorDialog/Dialog或PopupWindow,需要活动上下文。PFB返回警报对话框的示例代码:
return new AlertDialog.Builder(baseFragment.getActivity().getApplicationContext())
.setTitle("TITLE OF YOUR ALERT DIALOG")
.setMessage(“MESSAGE YOU WANT TO SHOW”)
.setCancelable(false)
.setPositiveButton("BUTTON NAME",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
removeDialog(The id of the managed dialog);
//TASK YOU WANT TO PERFORM
finish();
}
})
.setNegativeButton("BUTTON NAME",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
removeDialog(The id of the managed dialog);
finish();
}
}).create();
欢迎使用StackOverflow:)使用AlertDialog developer.android.com/reference/android/app/AlertDialog.html将上下文传递给非活动类构造函数,并使用该上下文显示“new AlertDialog.Builder(this).setTitle(“Title”).setMessage(“error:here”).show();”