2014-11-05 80 views
0

时出错,executin doinBackground(),请帮助............ 这是我的代码发生:Anerror在执行doinbackground

11-05 05:03:20.171: E/AndroidRuntime(3171): FATAL EXCEPTION: AsyncTask #1 
11-05 05:03:20.171: E/AndroidRuntime(3171): Process: com.example.mysqltest, PID: 3171 
11-05 05:03:20.171: E/AndroidRuntime(3171): java.lang.RuntimeException: An error occured while executing doInBackground() 
11-05 05:03:20.171: E/AndroidRuntime(3171):  at android.os.AsyncTask$3.done(AsyncTask.java:300) 
11-05 05:03:20.171: E/AndroidRuntime(3171):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
11-05 05:03:20.171: E/AndroidRuntime(3171):  at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
11-05 05:03:20.171: E/AndroidRuntime(3171):  at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
11-05 05:03:20.171: E/AndroidRuntime(3171):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
11-05 05:03:20.171: E/AndroidRuntime(3171):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
11-05 05:03:20.171: E/AndroidRuntime(3171):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
11-05 05:03:20.171: E/AndroidRuntime(3171):  at java.lang.Thread.run(Thread.java:841) 
11-05 05:03:20.171: E/AndroidRuntime(3171): Caused by: java.lang.NullPointerException 
+0

代码抛出空指针异常。你可以调试doinBackground方法吗?我的假设是,错误发生在post_message和post_title上。 – RAAAAM 2014-11-05 10:21:49

+0

发布您的完整日志猫 – 2014-11-05 10:29:48

+0

我已调试的代码有post_message和post_title.This时没有问题。这个时候我得到了这个错误........ – 2014-11-05 10:32:53

回答

0

中点击收听做到这一点

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


     } 

} 
+0

否...它仍然不适用于我.......... – 2014-11-05 10:45:42

+0

比你开始异步任务在错误的地方,你从自定义对话框调用,但你在错误的地方添加异步任务。 – 2014-11-05 10:51:02

+0

我从oncreate方法调用此函数.......其实我是新来的android和不知道这么多....请帮助,如果你可以? – 2014-11-05 11:06:31

相关问题