I try to combine 2 programs with copy and paste, that are date/time picker with asynch task background processing.
With this code:
@Override
public void onClick(View v) {
new PostComment().execute();
that executes asynch task with background processing.
If I put the above execute code on:
public class AddComment extends Activity implements OnClickListener{
and when I pressed date/time picker button, the program crashed.
If I put the above execute code on just above of:
class PostComment extends AsyncTask<String, String, String> {
I can do entry of date/time picker correctly, but when I pressed "submit" button to save the all entries, there is no response at all.
I thought the problem is related to the position and additional coding of the 'execute' line coding above.
Could any one help with this?
My android code is below :
public class AddComment extends Activity implements OnClickListener{
EditText fdate;
EditText tdate;
EditText ftime;
EditText ttime;
private EditText custbranch;
private EditText custname;
private EditText custaddr;
private EditText custcity;
private EditText note;
Button btnDate1;
Button btnDate2;
Button btnTime1;
Button btnTime2;
private Button mSubmit;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
//testing on Emulator:
private static final String POST_COMMENT_URL = "http://192.168.0.245
// Variable for storing current date and time
private int mYear, mMonth, mDay, mHour, mMinute;
//ids
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_comment);
btnDate1 = (Button) findViewById(R.id.btnDate1);
btnDate2 = (Button) findViewById(R.id.btnDate2);
btnTime1 = (Button) findViewById(R.id.btnTime1);
btnTime2 = (Button) findViewById(R.id.btnTime2);
fdate = (EditText) findViewById(R.id.fdate);
tdate = (EditText) findViewById(R.id.tdate);
ftime = (EditText) findViewById(R.id.ftime);
ttime = (EditText) findViewById(R.id.ttime);
btnDate1.setOnClickListener(this);
btnDate2.setOnClickListener(this);
btnTime1.setOnClickListener(this);
btnTime2.setOnClickListener(this);
mSubmit = (Button) findViewById(R.id.submit);
mSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == btnDate1) {
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd1 = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
fdate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd1.show();
}
if (v == btnDate2) {
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd2 = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
tdate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd2.show();
}
if (v == btnTime1) {
// Process to get Current Time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog tpd1 = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// Display Selected time in textbox
ftime.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
tpd1.show();
}
if (v == btnTime2) {
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Time Picker Dialog
TimePickerDialog tpd2 = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// Display Selected time in textbox
ttime.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
tpd2.show();
}
custbranch = (EditText)findViewById(R.id.custbranch);
custname = (EditText)findViewById(R.id.custname);
custaddr = (EditText)findViewById(R.id.custaddr);
custcity = (EditText)findViewById(R.id.custcity);
note = (EditText)findViewById(R.id.note);
mSubmit = (Button)findViewById(R.id.submit);
mSubmit.setOnClickListener(this);
}
public void onClickbtnSubmit(View v) {
new PostComment().execute();
}
class PostComment extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AddComment.this);
pDialog.setMessage("Posting Comment...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String post_fdate = fdate.getText().toString();
String post_tdate = tdate.getText().toString();
String post_ftime = ftime.getText().toString();
String post_ttime = ttime.getText().toString();
String post_custbranch = custbranch.getText().toString();
String post_custname = custname.getText().toString();
String post_custaddr = custaddr.getText().toString();
String post_custcity = custcity.getText().toString();
String post_note = note.getText().toString();
//We need to change this:
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("fdate", post_fdate));
params.add(new BasicNameValuePair("tdate", post_tdate));
params.add(new BasicNameValuePair("ftime", post_ftime));
params.add(new BasicNameValuePair("ttime", post_ttime));
params.add(new BasicNameValuePair("custbranch",post_custbranch));
params.add(new BasicNameValuePair("custname", post_custname));
params.add(new BasicNameValuePair("custaddr", post_custaddr));
params.add(new BasicNameValuePair("custcity", post_custcity));
params.add(new BasicNameValuePair("note", post_note));
Log.d("request!", "starting");
//Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(
POST_COMMENT_URL, "POST", params);
// full json response
Log.d("Post Comment attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Comment Added!", json.toString());
finish();
return json.getString(TAG_MESSAGE);
}else{
Log.d("Comment 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();
if (file_url != null){
Toast.makeText(AddComment.this,file_url,
Toast.LENGTH_LONG).show();
}
}
}
}
This is the logcat when crashed, when pressed date/time picker button.
08-04 15:22:19.480: E/AndroidRuntime(30627): FATAL EXCEPTION: AsyncTask #2 08-04 15:22:19.480: E/AndroidRuntime(30627): java.lang.RuntimeException: An error occured while executing doInBackground() 08-04 15:22:19.480: E/AndroidRuntime(30627): at android.os.AsyncTask$3.done(AsyncTask.java:299) 08-04 15:22:19.480: E/AndroidRuntime(30627): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 08-04 15:22:19.480: E/AndroidRuntime(30627): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 08-04 15:22:19.480: E/AndroidRuntime(30627): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 08-04 15:22:19.480: E/AndroidRuntime(30627): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 08-04 15:22:19.480: E/AndroidRuntime(30627): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 08-04 15:22:19.480: E/AndroidRuntime(30627): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 08-04 15:22:19.480: E/AndroidRuntime(30627): at java.lang.Thread.run(Thread.java:838) 08-04 15:22:19.480: E/AndroidRuntime(30627): Caused by: java.lang.NullPointerException 08-04 15:22:19.480: E/AndroidRuntime(30627): at com.lm.vciwhereabout.AddComment$PostComment.doInBackground(AddComment.java:244) 08-04 15:22:19.480: E/AndroidRuntime(30627): at com.lm.vciwhereabout.AddComment$PostComment.doInBackground(AddComment.java:1) 08-04 15:22:19.480: E/AndroidRuntime(30627): at android.os.AsyncTask$2.call(AsyncTask.java:287) 08-04 15:22:19.480: E/AndroidRuntime(30627): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 08-04 15:22:19.480: E/AndroidRuntime(30627): ... 4 more 08-04 15:22:21.400: E/WindowManager(30627): Activity com.lm.vciwhereabout.AddComment has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42b26020 V.E..... R......D 0,0-684,192} that was originally added here 08-04 15:22:21.400: E/WindowManager(30627): android.view.WindowLeaked: Activity com.lm.vciwhereabout.AddComment has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42b26020 V.E..... R......D 0,0-684,192} that was originally added here 08-04 15:22:21.400: E/WindowManager(30627): at android.view.ViewRootImpl.(ViewRootImpl.java:494) 08-04 15:22:21.400: E/WindowManager(30627): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:218) 08-04 15:22:21.400: E/WindowManager(30627): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:74) 08-04 15:22:21.400: E/WindowManager(30627): at android.app.Dialog.show(Dialog.java:322) 08-04 15:22:21.400: E/WindowManager(30627): at com.lm.vciwhereabout.AddComment$PostComment.onPreExecute(AddComment.java:232) 08-04 15:22:21.400: E/WindowManager(30627): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 08-04 15:22:21.400: E/WindowManager(30627): at android.os.AsyncTask.execute(AsyncTask.java:534) 08-04 15:22:21.400: E/WindowManager(30627): at com.lm.vciwhereabout.AddComment.onClick(AddComment.java:112) 08-04 15:22:21.400: E/WindowManager(30627): at android.view.View.performClick(View.java:4336) 08-04 15:22:21.400: E/WindowManager(30627): at android.view.View$PerformClick.run(View.java:17853) 08-04 15:22:21.400: E/WindowManager(30627): at android.os.Handler.handleCallback(Handler.java:800) 08-04 15:22:21.400: E/WindowManager(30627): at android.os.Handler.dispatchMessage(Handler.java:100) 08-04 15:22:21.400: E/WindowManager(30627): at android.os.Looper.loop(Looper.java:194) 08-04 15:22:21.400: E/WindowManager(30627): at android.app.ActivityThread.main(ActivityThread.java:5469) 08-04 15:22:21.400: E/WindowManager(30627): at java.lang.reflect.Method.invokeNative(Native Method) 08-04 15:22:21.400: E/WindowManager(30627): at java.lang.reflect.Method.invoke(Method.java:525) 08-04 15:22:21.400: E/WindowManager(30627): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:857) 08-04 15:22:21.400: E/WindowManager(30627): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624) 08-04 15:22:21.400: E/WindowManager(30627): at dalvik.system.NativeStart.main(Native Method)
The problem related to the sqllite db data definition .
Problem solved, after changed the format of the date on data base from dd-mm-yy to yy-mm-dd. It's cause NPE before.