【问题标题】:An error occurred while executing doinbackground执行 doinbackground 时出错
【发布时间】:2015-01-01 11:43:48
【问题描述】:

执行doinBackground()时出错。

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(AddComment.this);
        pDialog.setMessage("Attempting login...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
        //postData("deepika");
    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        int success;
        String post_title = intime.getText().toString();
        String post_message = outtime.getText().toString();
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(AddComment.this);
        String post_username = sp.getString("username", "anon");
        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", post_username));
            params.add(new BasicNameValuePair("intime", post_title));
            params.add(new BasicNameValuePair("outtime", post_message));

            Log.d("request!", "starting");
            // getting product details by making HTTP request
            JSONObject json = jsonParser.makeHttpRequest(POST_COMMENT_URL, "POST",
                    params);
            //JSONObject json1 = jsonParser.makeHttpRequest("http://192.168.10.30/webservice/comments.php", "POST",
                //  params);
            // check your log for json response
            Log.d("Login attempt", json.toString());

            // json success tag
            success = json.getInt(TAG_SUCCESS);
        if (success == 1) {     

                Log.d("Attendence Marked!", json.toString());    
                //finish();
                return json.getString(TAG_MESSAGE);
            }else{
                Log.d("Attendence Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;

    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        finish();
        if (file_url != null) {
            Toast.makeText(AddComment.this, file_url, Toast.LENGTH_LONG).show();
        }
    }
} 

这是错误日志:

11-05 05:03:20.171:E/AndroidRuntime(3171):致命异常:AsyncTask #1
11-05 05:03:20.171: E/AndroidRuntime(3171): 进程: com.example.mysqltest, PID: 3171
11-05 05:03:20.171: E/AndroidRuntime(3171): java.lang.RuntimeException: 执行 doInBackground() 时出错
11-05 05:03:20.171: E/AndroidRuntime(3171): 在 android.os.AsyncTask$3.done(AsyncTask.java:300)
11-05 05:03:20.171: E/AndroidRuntime(3171): 在 java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
11-05 05:03:20.171: E/AndroidRuntime(3171): 在 java.util.concurrent.FutureTask.setException(FutureTask.java:222)
11-05 05:03:20.171: E/AndroidRuntime(3171): 在 java.util.concurrent.FutureTask.run(FutureTask.java:242)
11-05 05:03:20.171: E/AndroidRuntime(3171): 在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-05 05:03:20.171: E/AndroidRuntime(3171): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-05 05:03:20.171: E/AndroidRuntime(3171): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-05 05:03:20.171: E/AndroidRuntime(3171): 在 java.lang.Thread.run(Thread.java:841)
11-05 05:03:20.171: E/AndroidRuntime(3171): 引起: java.lang.NullPointerException

【问题讨论】:

  • 代码抛出空指针异常。你能调试 doinBackground 方法吗?我的假设是,错误发生在 post_message 和 post_title 上。
  • 发布你的完整日志猫
  • 我已经调试过代码post_message和post_title没有问题。这次我遇到了这个错误............
  • AddComment 已泄露窗口 com.android.internal.policy.impl.PhoneWindow$DecorView{b2edb850 VE.... R.....ID 0,0-456,144} 最初添加在这里11-05 05:30:40.041: E/WindowManager(3305): 在 android.view.ViewRootImpl.(ViewRootImpl.java:348) 11-05 05:30:40.041: E/WindowManager(3305): 在
  • 你能分享整个日志吗?我认为空指针异常是问题所在?

标签: android


【解决方案1】:

在点击监听器中这样做

 mSubmit.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View vw) {
                    String post_title = intime.getText().toString();
                    String post_message = outtime.getText().toString();

                    new PostComment(post_title,post_message).execute();
                }
            }); 



and in the asynch task change this
class PostComment extends AsyncTask<String, String, String> {

            String response = "";
            String post_title ="";
            String post_message="";
            // Check for success tag
            int success;


            public PostComment(String title,String msg) {
                post_title = title;
                post_message = msg;
            }

      @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AddComment.this);
            pDialog.setMessage("Attempting login...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
            //postData("deepika");
        }



     @Override
        protected String doInBackground(String... args) {


            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(AddComment.this);
            String post_username = sp.getString("username", "anon");
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", post_username));
                params.add(new BasicNameValuePair("intime", post_title));
                params.add(new BasicNameValuePair("outtime", post_message));

                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(POST_COMMENT_URL, "POST",
                        params);
                //JSONObject json1 = jsonParser.makeHttpRequest("http://192.168.10.30/webservice/comments.php", "POST",
                    //  params);
                // check your log for json response
                Log.d("Login attempt", json.toString());

                // json success tag
                success = json.getInt(TAG_SUCCESS);

    if (success == 1) {

                    Log.d("Attendence Marked!", json.toString());    

                    response = json.getString(TAG_MESSAGE);
                }else{
                    Log.d("Attendence Failure!", json.getString(TAG_MESSAGE));
                    response = json.getString(TAG_MESSAGE);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }catch (Exception e) {
                e.printStackTrace();
            }

            return response;

        }

        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();

            if (file_url != null) {
                Toast.makeText(AddComment.this, file_url, Toast.LENGTH_LONG).show();
            }


        }

}

【讨论】:

  • 比您在从自定义对话框调用的错误位置启动异步任务,但您在错误位置添加了异步任务。
  • 我从 oncreate 方法调用这个函数.......其实我是 android 新手,不太了解......如果可以请帮忙???跨度>
  • 受保护的 void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_comment); btnCTime=(按钮)findViewById(R.id.button1); btnCTime2=(按钮)findViewById(R.id.button4); btnCTime2.setVisibility(View.GONE); intime = (EditText)findViewById(R.id.title); intime.setInputType(InputType.TYPE_NULL); mSubmit = (Button)findViewById(R.id.submit); mSubmit.setOnClickListener(new OnClickListener() { @Override public void onClick(View vw) { new PostComment().execute(); } });
  • 不要使用finish() onPost() 方法,检查它是否有效?
  • 是的,我明白了,问题实际上是我这样设置 intime Editbox:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 2020-02-09
  • 2021-04-16
  • 1970-01-01
相关资源
最近更新 更多