Android:Datepicker对话框导致崩溃?

时间:2015-02-03 16:34:07

标签: android datepicker

我制作了一个将日期发送到mysql的Android应用程序。它很成功,然后我尝试制作一个标签小部件。这两个选项卡包含相同的代码,但现在是两个不同的类,在执行此操作后,日期选择器对话框在激活时开始崩溃。

导致这种情况的原因是什么?

logcat报告

02-03 16:32:15.797: E/AndroidRuntime(1445): FATAL EXCEPTION: main
02-03 16:32:15.797: E/AndroidRuntime(1445): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@533459e0 is not valid; is your activity running?
02-03 16:32:15.797: E/AndroidRuntime(1445):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at android.app.Dialog.show(Dialog.java:281)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at com.example.androidtablayout.VacLeave$2.onClick(VacLeave.java:129)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at android.view.View.performClick(View.java:4204)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at android.view.View$PerformClick.run(View.java:17355)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at android.os.Handler.handleCallback(Handler.java:725)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at android.os.Looper.loop(Looper.java:137)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at java.lang.reflect.Method.invoke(Method.java:511)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-03 16:32:15.797: E/AndroidRuntime(1445):     at dalvik.system.NativeStart.main(Native Method)

datepicker的代码 VacLeave.java

 public class VacLeave extends Activity implements OnClickListener {
    Button btnCreateProduct;
    ListView listView;
String[] trans;

// Session Manager Class
SessionManager session;

JSONParser jsonParser = new JSONParser();

ProgressDialog mProgressDialog;
JSONArray jsonarray;

EditText inputName;
String username;
EditText inputStart;
EditText inputEnd;
Button btnLogout;
Calendar myCalendar = Calendar.getInstance();

private String url = "http://10.0.3.2/sunshine-ems/vac.php";


private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.leave);

    inputName = (EditText) findViewById(R.id.inputName);
    inputStart = (EditText) findViewById(R.id.inputStart);
    inputEnd = (EditText) findViewById(R.id.inputEnd);
    btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);





    inputStart.setOnClickListener(new OnClickListener() {

        public void onClick(View view)


        {
            int mYear, mMonth, mDay;

            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog dpd = new DatePickerDialog(VacLeave.this,
                new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker view, int year, int month, int day) {

                        c.set(year, month, day);
                        String date = new SimpleDateFormat("yyyy/MM/dd").format(c.getTime());
                        inputStart.setText(date);

                        int mYear = c.get(Calendar.YEAR);
                        int mMonth = c.get(Calendar.MONTH);
                        int mDay = c.get(Calendar.DAY_OF_MONTH);

                         }
                }, mYear, mMonth, mDay);
            if(!((Activity) VacLeave.this).isFinishing())
            {
                dpd.show();
            }
        }
    });

    inputEnd.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            int mYear, mMonth, mDay;

            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog dpd = new DatePickerDialog(VacLeave.this,
                new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker view, int year, int month, int day) {

                        c.set(year, month, day);
                        String date = new SimpleDateFormat("yyyy/MM/dd").format(c.getTime());
                        inputEnd.setText(date);

                        int mYear = c.get(Calendar.YEAR);
                        int mMonth = c.get(Calendar.MONTH);
                        int mDay = c.get(Calendar.DAY_OF_MONTH);

                    }
                }, mYear, mMonth, mDay);
            if(!((Activity) VacLeave.this).isFinishing())
            {
                dpd.show();
            }
        }
    });
    // CHECK TO LOGIN SESSION
    session = new SessionManager(getApplicationContext());
    session.checkLogin();
    btnLogout = (Button) findViewById(R.id.btnLogout);;
    btnLogout.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    // Launching All products Activity
                    session.logoutUser();
                    Intent i = new Intent(getApplicationContext(), Login.class);
                    startActivity(i);

                }
            });

    btnCreateProduct.setOnClickListener(this);
}


private class Submit extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(VacLeave.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Processing Leave request");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {

        int success;
        Spinner mySpinner=(Spinner) findViewById(R.id.spinner);
        username = session.getUsername();

        List<NameValuePair> params1 = new ArrayList<NameValuePair>();

        String reason = inputName.getText().toString();
        String start = inputStart.getText().toString();
        String end = inputEnd.getText().toString();
        String status ="pending";
        String leavetype="vacation";
        //String leavetype = mySpinner.getSelectedItem().toString();
        try {

            params1.add(new BasicNameValuePair("reason", reason));
            params1.add(new BasicNameValuePair("username", username));
            params1.add(new BasicNameValuePair("start", start));
            params1.add(new BasicNameValuePair("end", end));
            params1.add(new BasicNameValuePair("leavetype", leavetype));
            params1.add(new BasicNameValuePair("status", status));
            Log.d("request!", "starting");

            JSONObject json = jsonParser.makeHttpRequest(url, "POST",
                    params1);

            // full json response
            Log.d("Payment attempt", json.toString());

            // json success element
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {

                return json.getString(TAG_MESSAGE);

            } else {
                Log.d("Login 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
        mProgressDialog.dismiss();
        if (file_url != null) {
            Toast.makeText(VacLeave.this, file_url, Toast.LENGTH_LONG).show();
        }
    }

}

/**
 * Logout button click event
 * */
public void onClick(View v) {

    switch (v.getId()) {

    case R.id.btnCreateProduct:
        if ( checkValidation () )
            new Submit().execute();
        else
            Toast.makeText(VacLeave.this, "Required field(s)", Toast.LENGTH_LONG).show();


    }

}
private boolean checkValidation() {
    boolean ret = true;

    if (!Validation.hasText(inputName)) ret = false;
    if (!Validation.hasText(inputStart)) ret = false;
    if (!Validation.hasText(inputEnd)) ret = false;



    return ret;
}

}

1 个答案:

答案 0 :(得分:1)

当尝试显示对话框时,调用对话框的活动由于某种原因而完成时会发生这种情况。

这是你可以做的

替换

dpd.show();

if(!((Activity) SickLeave.this).isFinishing())
{
    dpd.show();
}