致命异常:AsyncTask的#1 doInBackground [英] fatal exception : asynctask #1 doInBackground

查看:260
本文介绍了致命异常:AsyncTask的#1 doInBackground的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林新到Android编程,我不能查明这会触发错误。我有这样的错误:

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)

,这些都是我的codeS类中的AsyncTask的:

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

        }
    }
}

请帮忙:((

推荐答案

不要执行的UI操作相关doInBackgroungd()方法。只返回结果 onPostExecute(),然后开始写活动的code。

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
        }

    }

这篇关于致命异常:AsyncTask的#1 doInBackground的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆