Android异步任务在后台错误中执行

时间:2014-02-06 07:35:12

标签: android

尝试创建连接到本地主机上的数据库的活动,在用户填写表单后,使用后台方法中的asyntask.do将表单提交给localhost。我收到错误并且不太确定有什么问题,请我帮忙。

这是我的logcat

  `01-30 06:03:10.451: E/AndroidRuntime(1332): FATAL EXCEPTION: AsyncTask #1
    01-30 06:03:10.451: E/AndroidRuntime(1332): java.lang.RuntimeException: An error occured while executing doInBackground()
01-30 06:03:10.451: E/AndroidRuntime(1332):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
01-30 06:03:10.451: E/AndroidRuntime(1332):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-30 06:03:10.451: E/AndroidRuntime(1332):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-30 06:03:10.451: E/AndroidRuntime(1332):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-30 06:03:10.451: E/AndroidRuntime(1332):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-30 06:03:10.451: E/AndroidRuntime(1332):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
01-30 06:03:10.451: E/AndroidRuntime(1332):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-30 06:03:10.451: E/AndroidRuntime(1332):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-30 06:03:10.451: E/AndroidRuntime(1332):     at java.lang.Thread.run(Thread.java:856)
01-30 06:03:10.451: E/AndroidRuntime(1332): Caused by: java.lang.NullPointerException
01-30 06:03:10.451: E/AndroidRuntime(1332):     at com.oose.buddiev2.RegisterActivity$CreateNewUser.doInBackground(RegisterActivity.java:101)
01-30 06:03:10.451: E/AndroidRuntime(1332):     at com.oose.buddiev2.RegisterActivity$CreateNewUser.doInBackground(RegisterActivity.java:1)
01-30 06:03:10.451: E/AndroidRuntime(1332):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-30 06:03:10.451: E/AndroidRuntime(1332):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-30 06:03:10.451: E/AndroidRuntime(1332):     ... 5 more`

这是我在活动中的异步任务

class CreateNewUser extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(RegisterActivity.this);
        pDialog.setMessage("Creating User..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating User
     * */
    protected String doInBackground(String... args) {

        String usernameString = usernameEditText.getText().toString();
        String emailString = emailEditText.getText().toString();
        String passwordString = passwordEditText.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("username", usernameString));
        params.add(new BasicNameValuePair("email", emailString));
        params.add(new BasicNameValuePair("password", passwordString));

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

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

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

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

                // closing this screen
                finish();
            } else {
                // failed to create product
            }
        } 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();
    }

1 个答案:

答案 0 :(得分:0)

你不能在doInBackground中这样做。

尝试从doInBackground中删除它,或者你可以把它放在OnPostExecute()中:

startActivity(i);
finish();