按下 date/ime 选择器时崩溃,结合后台处理

Crashed when pressed date/ime picker, combined with background processing

我尝试将 2 个程序与复制和粘贴相结合,即 date/time 具有异步任务后台处理的选择器。

使用此代码:

@Override
public void onClick(View v) {
new PostComment().execute();

通过后台处理执行异步任务。

如果我把上面的执行代码放在:

public class AddComment extends Activity implements OnClickListener{ 

当我按下 date/time 选择器按钮时,程序崩溃了。

如果我把上面的执行代码放在上面:

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

我可以正确输入 date/time 选择器,但是当我按下 "submit" 按钮保存所有条目时,根本没有任何反应。

我认为问题与上面'execute'行编码的位置和附加编码有关。

有人可以帮忙吗?

我的 android 代码如下:

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

}

}
}

这是崩溃时的 logcat,当按下 date/time 选择器按钮时。

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.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.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.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)

与 sqllite 数据库数据定义相关的问题。

问题已解决,将数据库中 日期 的格式从 dd-mm-yy 更改为 yy-mm-dd。之前是NPE的原因。