无法在AsyncTask的onPostExecute方法

时间:2015-07-13 10:00:05

标签: java android

我现在2天以来一直在努力解决这个错误,我无法弄清楚如何修复它。 我有一个调用web服务的产品的订购方法,我解析响应,如果响应是否定的,我必须在onPostExecute方法中显示一个AlertDialog。这是我正在使用的代码:

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();
        }

    }

当应用程序到达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)

有谁知道错误是什么?

3 个答案:

答案 0 :(得分:1)

为什么要创建2次AlertDialog?使用Builder,您可以显示它。更改以下代码并正常工作。 替换

Python Exception <class 'gdb.error'> Cannot execute this command while the selected thread is running.:

AlertDialog.Builder reorder = new AlertDialog.Builder(myActivity.this);


删除

AlertDialog.Builder reorder = new AlertDialog.Builder(myActivity.this).create();

并添加

AlertDialog orderError = reorder.create();
orderError.show();

多数民众赞成......

答案 1 :(得分:0)

确保在使用之前初始化了“ urlTest ”。

答案 2 :(得分:0)

试试这个。

 let today = NSDate()
 println(today)