fatal exception : asynctask #1 doInBackground

跟風遠走 提交于 2020-01-24 20:31:06

问题


Im new to android programming and i cant pinpoint which triggers the error. i am having this errors:

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)

and these are my codes in the class 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();

        }
    }
}

please help :((


回答1:


Do not perform the UI related operation in doInBackgroungd() method. Just return the result in onPostExecute() and then write the code of start activity.

    /**
     * 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
        }

    }



回答2:


// 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
            }

        }
    }


来源:https://stackoverflow.com/questions/19651428/fatal-exception-asynctask-1-doinbackground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!