我有一个asyncTask
,并且我想在触发alertDialog
方法时显示一个onPostExecute
。
我声明了一个Context变量,并在OnCreate
方法中将其初始化为:
mContext = this;
然后为了在onPostExecute方法上显示
alertDialog
,我使用了以下代码: AlertDialog.Builder goLogin = new AlertDialog.Builder(mContext);
goLogin.setMessage("test");
goLogin.setCancelable(false);
goLogin.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertLogin = goLogin.create();
alertLogin.show();
但是我得到的是以下错误:
07-10 14:42:09.710: E/AndroidRuntime(12963): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:148)
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:99)
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:154)
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:379)
07-10 14:42:09.710: E/AndroidRuntime(12963): at test.test.showAlert(test.java:671)
07-10 14:42:09.710: E/AndroidRuntime(12963): at test.test.checkResult(test.java:656)
07-10 14:42:09.710: E/AndroidRuntime(12963): at test.test$login.onPostExecute(test.java:585)
07-10 14:42:09.710: E/AndroidRuntime(12963): at test.test$login.onPostExecute(test.java:1)
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.os.AsyncTask.finish(AsyncTask.java:632)
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.os.Handler.dispatchMessage(Handler.java:102)
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.os.Looper.loop(Looper.java:135)
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.app.ActivityThread.main(ActivityThread.java:5312)
07-10 14:42:09.710: E/AndroidRuntime(12963): at java.lang.reflect.Method.invoke(Native Method)
07-10 14:42:09.710: E/AndroidRuntime(12963): at java.lang.reflect.Method.invoke(Method.java:372)
07-10 14:42:09.710: E/AndroidRuntime(12963): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
07-10 14:42:09.710: E/AndroidRuntime(12963): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
我究竟做错了什么?我是否需要以其他方式传递上下文?
编辑:
整个AsyncTask代码:
private class login extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
}
@SuppressWarnings("deprecation")
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userName", "test"));
String jsonStr = sh.makeServiceCall(urlTest, ServiceHandler.POST, params);
if (jsonStr != null) {
try {
JSONObject obj = new JSONObject(jsonStr);
error = obj.getBoolean("Error");
if(!error)
{
loginResult = true;
JSONObject array = obj.getJSONObject("Response");
name = array.getString("Name");
}
else
{
loginResult = false;
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
//checkResult();
AlertDialog.Builder reorder = new AlertDialog.Builder(context);
reorder.setMessage("error");
reorder.setCancelable(true);
reorder.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog orderError = reorder.create();
orderError.show();
}
}
最佳答案
创建AsyncTask的构造函数,然后执行以下操作:
private class DemoTask extends AsyncTask<Void, Void, Void> {
Context context;
public DemoTask(Context mContext) {
this.context = mContext;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
AlertDialog.Builder goLogin = new AlertDialog.Builder(context);
goLogin.setMessage("test");
goLogin.setCancelable(false);
goLogin.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertLogin = goLogin.create();
alertLogin.show();
}
}
这里
mContext
在onCreate
方法内部:public class MyActivity extends Activity {
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
new DemoTask(mContext).execute();
}
关于java - 在Android的AsyncTask的onPostExecute上显示alertDialog,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31340732/