按下日期/时间选择器时崩溃,并结合后台处理

时间:2015-08-04 07:37:11

标签: android background-process datetimepicker buttonclick

我尝试将两个程序与复制和粘贴相结合,即日期/时间选择器与异步任务后台处理。

使用此代码:

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

使用后台处理执行异步任务。

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

public class AddComment extends Activity implements OnClickListener{ 

当我按下日期/时间选择器按钮时,程序崩溃了。

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

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

我可以正确输入日期/时间选择器,但是当我按下&#34;提交&#34;按钮保存所有条目,根本没有响应。

我认为这个问题与“执行”的位置和附加编码有关。上面的线编码。

任何人都可以帮忙吗?

我的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。

  

08-04 15:22:19.480:E / AndroidRuntime(30627):致命异常:   AsyncTask#2 08-04 15:22:19.480:E / AndroidRuntime(30627):   java.lang.RuntimeException:执行时发生错误   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):引起: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更多08-04 15:22:21.400:   E / WindowManager(30627):活动com.lm.vciwhereabout.AddComment具有   泄露的窗户   com.android.internal.policy.impl.PhoneWindow $ {DecorView 42b26020   V.E ..... R ...... D 0,0-684,192}最初在这里添加08-04   15:22:21.400:E / WindowManager(30627):android.view.WindowLeaked:   活动com.lm.vciwhereabout.AddComment已泄露窗口   com.android.internal.policy.impl.PhoneWindow $ {DecorView 42b26020   V.E ..... R ...... D 0,0-684,192}最初在这里添加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(原生方法)

1 个答案:

答案 0 :(得分:0)

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

在将数据库中日期的格式从dd-mm-yy更改为yy-mm-dd后,问题解决了。它之前引起了NPE。