[英]Cannot show Toast nor AlertDialog on AsyncTask's onPostExecute method
I'm struggling with this error since 2 days now and I can't figure out how to fix it. 自2天以来,我一直在努力解决此错误,而且我不知道该如何解决。 I have an order method for a product that calls a webservice, I parse the response and if the response is negative I have to show an AlertDialog inside the onPostExecute method.
我有一个用于调用Web服务的产品的订购方法,我解析响应,如果响应为负,则必须在onPostExecute方法内显示AlertDialog。 This is the code i'm using:
这是我正在使用的代码:
private class test extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@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("deviceOS", "Android"));
String jsonStr = sh.makeServiceCall(urlTest, ServiceHandler.POST, params);
if (jsonStr != null) {
try {
JSONObject obj = new JSONObject(jsonStr);
error = obj.getBoolean("Error");
if(!error)
{
test = true;
JSONObject array = obj.getJSONObject("Response");
token = array.getString("Token");
}
else
{
test = 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(myActivity.this);
reorder.setMessage("test");
reorder.setCancelable(true);
reorder.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog orderError = reorder.create();
orderError.show();
}
}
When the app reaches the onPostExecute method it crashes and the log is this: 当应用程序到达onPostExecute方法时,它将崩溃,并且日志如下:
07-13 11:58:31.074: E/AndroidRuntime(2529): FATAL EXCEPTION: main
07-13 11:58:31.074: E/AndroidRuntime(2529): Process: com.test.Test, PID: 2529
07-13 11:58:31.074: E/AndroidRuntime(2529): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
07-13 11:58:31.074: E/AndroidRuntime(2529): at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:148)
07-13 11:58:31.074: E/AndroidRuntime(2529): at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:99)
07-13 11:58:31.074: E/AndroidRuntime(2529): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:154)
07-13 11:58:31.074: E/AndroidRuntime(2529): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:379)
07-13 11:58:31.074: E/AndroidRuntime(2529): at test.test$login.onPostExecute(test.java:575)
07-13 11:58:31.074: E/AndroidRuntime(2529): at test.test$test.onPostExecute(test.java:1)
07-13 11:58:31.074: E/AndroidRuntime(2529): at android.os.AsyncTask.finish(AsyncTask.java:632)
07-13 11:58:31.074: E/AndroidRuntime(2529): at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-13 11:58:31.074: E/AndroidRuntime(2529): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
07-13 11:58:31.074: E/AndroidRuntime(2529): at android.os.Handler.dispatchMessage(Handler.java:102)
07-13 11:58:31.074: E/AndroidRuntime(2529): at android.os.Looper.loop(Looper.java:135)
07-13 11:58:31.074: E/AndroidRuntime(2529): at android.app.ActivityThread.main(ActivityThread.java:5312)
07-13 11:58:31.074: E/AndroidRuntime(2529): at java.lang.reflect.Method.invoke(Native Method)
07-13 11:58:31.074: E/AndroidRuntime(2529): at java.lang.reflect.Method.invoke(Method.java:372)
07-13 11:58:31.074: E/AndroidRuntime(2529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
07-13 11:58:31.074: E/AndroidRuntime(2529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Does anyone know what the error could be? 有谁知道错误可能是什么?
Why are you create 2 times AlertDialog? 为什么要创建2次AlertDialog? Using Builder you can show it.
使用Builder可以显示它。 Change Below code and Works fine.
更改以下代码并正常工作。 Replace
更换
AlertDialog.Builder reorder = new AlertDialog.Builder(myActivity.this);
with 与
AlertDialog.Builder reorder = new AlertDialog.Builder(myActivity.this).create();
and 和
Remove 去掉
AlertDialog orderError = reorder.create();
orderError.show();
and add 并添加
reorder.show();
Thats it... 而已...
使用前,请确保已初始化“ urlTest ”。
Try this. 尝试这个。
private class test extends AsyncTask<Void, Void, Void> {
WeakReference<Activity> weakActivity;
public test(Activity activity) {
weakActivity = activity;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
//blah blah code!
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Activity activity = weakActivity.get();
if (activity != null) {
// Dismiss the progress dialog
//checkResult();
AlertDialog.Builder reorder = new AlertDialog.Builder(activity);
reorder.setMessage("test");
reorder.setCancelable(true);
reorder.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog orderError = reorder.create();
orderError.show();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.