致命异常:asynctask#1 doInBackground

时间:2013-10-29 06:43:39

标签: android android-asynctask

我是android编程的新手,我无法确定哪个触发了错误。我有这个错误:

10-29 02:34:17.989: E/AndroidRuntime(853): FATAL EXCEPTION: AsyncTask #1
10-29 02:34:17.989: E/AndroidRuntime(853): java.lang.RuntimeException: An error occured while executing doInBackground()
10-29 02:34:17.989: E/AndroidRuntime(853):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-29 02:34:17.989: E/AndroidRuntime(853):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-29 02:34:17.989: E/AndroidRuntime(853):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-29 02:34:17.989: E/AndroidRuntime(853):  at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-29 02:34:17.989: E/AndroidRuntime(853):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-29 02:34:17.989: E/AndroidRuntime(853):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-29 02:34:17.989: E/AndroidRuntime(853):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-29 02:34:17.989: E/AndroidRuntime(853):  at java.lang.Thread.run(Thread.java:841)
10-29 02:34:17.989: E/AndroidRuntime(853): Caused by: java.lang.NullPointerException
10-29 02:34:17.989: E/AndroidRuntime(853):  at com.agf.tripassigner.CreateTripActivity$CreateTrip.doInBackground(CreateTripActivity.java:104)
10-29 02:34:17.989: E/AndroidRuntime(853):  at com.agf.tripassigner.CreateTripActivity$CreateTrip.doInBackground(CreateTripActivity.java:1)
10-29 02:34:17.989: E/AndroidRuntime(853):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-29 02:34:17.989: E/AndroidRuntime(853):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-29 02:34:17.989: E/AndroidRuntime(853):  ... 4 more
10-29 02:34:18.848: W/EGL_emulation(853): eglSurfaceAttrib not implemented
10-29 02:34:19.909: E/WindowManager(853): Activity com.agf.tripassigner.CreateTripActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41844d20 V.E..... R......D 0,0-456,144} that was originally added here
10-29 02:34:19.909: E/WindowManager(853): android.view.WindowLeaked: Activity com.agf.tripassigner.CreateTripActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41844d20 V.E..... R......D 0,0-456,144} that was originally added here
10-29 02:34:19.909: E/WindowManager(853):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
10-29 02:34:19.909: E/WindowManager(853):   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
10-29 02:34:19.909: E/WindowManager(853):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-29 02:34:19.909: E/WindowManager(853):   at android.app.Dialog.show(Dialog.java:281)
10-29 02:34:19.909: E/WindowManager(853):   at com.agf.tripassigner.CreateTripActivity$CreateTrip.onPreExecute(CreateTripActivity.java:80)
10-29 02:34:19.909: E/WindowManager(853):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-29 02:34:19.909: E/WindowManager(853):   at android.os.AsyncTask.execute(AsyncTask.java:534)
10-29 02:34:19.909: E/WindowManager(853):   at com.agf.tripassigner.CreateTripActivity$1.onClick(CreateTripActivity.java:58)
10-29 02:34:19.909: E/WindowManager(853):   at android.view.View.performClick(View.java:4240)
10-29 02:34:19.909: E/WindowManager(853):   at android.view.View$PerformClick.run(View.java:17721)
10-29 02:34:19.909: E/WindowManager(853):   at android.os.Handler.handleCallback(Handler.java:730)
10-29 02:34:19.909: E/WindowManager(853):   at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 02:34:19.909: E/WindowManager(853):   at android.os.Looper.loop(Looper.java:137)
10-29 02:34:19.909: E/WindowManager(853):   at android.app.ActivityThread.main(ActivityThread.java:5103)
10-29 02:34:19.909: E/WindowManager(853):   at java.lang.reflect.Method.invokeNative(Native Method)
10-29 02:34:19.909: E/WindowManager(853):   at java.lang.reflect.Method.invoke(Method.java:525)
10-29 02:34:19.909: E/WindowManager(853):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-29 02:34:19.909: E/WindowManager(853):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-29 02:34:19.909: E/WindowManager(853):   at dalvik.system.NativeStart.main(Native Method)

这些是我在asynctask类中的代码:

class CreateTrip extends AsyncTask<String, String, String> {
        /**
         * Before Sttarting Background thread Show Progress Dialog
         */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(CreateTripActivity.this);
            pDialog.setMessage("Creating Trip...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }
        /**
         * Creating Trip
         */
        protected String doInBackground(String... args) {
            String plate = inputPlate.getText().toString();
            String customer = inputCustomer.getText().toString();
            String area = inputArea.getText().toString();
            String driver = inputDriver.getText().toString();

            //Building parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("plate", plate));
            params.add(new BasicNameValuePair("customer", customer));
            params.add(new BasicNameValuePair("area", area));
            params.add(new BasicNameValuePair("driver", driver));

            //getting JSON Object
            // Note that create trip url accepts post method
            JSONObject json = jsonParser.makeHttpRequest(url_create_trip, "POST", params);

            // check log cat for response
            Log.d("Create Response", json.toString());

            // check for success tag
            try{
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1){
                    //successfully created trip 
                    Intent i = new Intent(getApplicationContext(), CreateTripActivity.class);
                    startActivity(i);

                    //closing this screen
                    finish();

                }else {
                    // failed to create trip
                }
            } catch (JSONException e){
                e.printStackTrace();
            }
            return null;
        }
        /**
         * After Completing background task Dismiss the progress Dialog
         */
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();

        }
    }
}

请帮助:((

2 个答案:

答案 0 :(得分:5)

请勿在{{1​​}}方法中执行与UI相关的操作。只需在doInBackgroungd()中返回结果,然后编写启动活动的代码。

onPostExecute()

答案 1 :(得分:1)

// try this way
class CreateTrip extends AsyncTask {
        ProgressDialog pDialog;
        /** * Before Sttarting Background thread Show Progress Dialog */
        @Override protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(CreateTripActivity.this);
            pDialog.setMessage("Creating Trip..."); pDialog.setIndeterminate(false);
            pDialog.setCancelable(true); pDialog.show();
        }
        /**
         * Creating Trip
         */
        protected String doInBackground(String... args) {
            String plate = inputPlate.getText().toString();
            String customer = inputCustomer.getText().toString();
            String area = inputArea.getText().toString();
            String driver = inputDriver.getText().toString();

            //Building parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("plate", plate));
            params.add(new BasicNameValuePair("customer", customer));
            params.add(new BasicNameValuePair("area", area));
            params.add(new BasicNameValuePair("driver", driver));

            //getting JSON Object
            // Note that create trip url accepts post method
            JSONObject json = jsonParser.makeHttpRequest(url_create_trip, "POST", params);

            // check log cat for response
            Log.d("Create Response", json.toString());

            // check for success tag
            try{
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1){
                    // success to create trip
                    return "1";
                }else {
                    return "0";
                    // failed to create trip
                }
            } catch (JSONException e){
                e.printStackTrace();
            }
            return null;
        }
        /**
         * After Completing background task Dismiss the progress Dialog
         */
        protected void onPostExecute(String result) {
            // dismiss the dialog once done
            pDialog.dismiss();
            if(result!=null && result.equals("1")){
                // success to create trip
                Intent i = new Intent(getApplicationContext(), CreateTripActivity.class);
                startActivity(i);
                finish();
            }else{
                // failed to create trip
            }

        }
    }