执行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):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):在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):在java.lang.Thread.run(Thread.java:841)
11-05 05:03:20.171:E / AndroidRuntime(3171):引起:java.lang.NullPointerException
答案 0 :(得分: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();
}
}
}