Android 在添加一些装置时,我试图将日期发送到db,但出现以下错误。应用程序正在崩溃。下面是代码
Java代码:Android 在添加一些装置时,我试图将日期发送到db,但出现以下错误。应用程序正在崩溃。下面是代码,android,parsing,datepicker,Android,Parsing,Datepicker,Java代码: public class ForgotPinCode extends AppCompatActivity { public static TextView SelectedDateView; public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override
public class ForgotPinCode extends AppCompatActivity {
public static TextView SelectedDateView;
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
SelectedDateView.setText(" " + (month + 1) + "-" + day + "-" + year);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_pin_code);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
android.support.v7.app.ActionBar ab = getSupportActionBar();
// Enable the Up button
if (ab!=null){
ab.setHomeAsUpIndicator(R.drawable.ic_back);
ab.setDisplayHomeAsUpEnabled(true);
ab.setTitle("Forgot PinCode");
}
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
SelectedDateView = (TextView) findViewById(R.id.forgot_pincode_DatePickerInput);
StringBuilder sb = new StringBuilder();
sb.append(day);
sb.append("/");
sb.append(month + 1);
sb.append("/");
sb.append(year - 18);
String a = sb.toString();
SelectedDateView.setText(a);
SelectedDateView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (hasFocus) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
} }
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
@SuppressLint("NewApi")
public void showDatePicker(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
public void SubmitRequest(View v) throws InterruptedException, ExecutionException {
EditText t = (EditText) findViewById(R.id.forgot_pincode_username);
String username = t.getText().toString();
t = (EditText) findViewById(R.id.forgot_pincode_password);
String password = t.getText().toString();
t = (EditText) findViewById(R.id.forgot_pincode_mobile);
String mobile = t.getText().toString();
TextView t1 = (TextView) findViewById(R.id.forgot_pincode_DatePickerInput);
String DOB = t1.getText().toString();
StringTokenizer st = new StringTokenizer(DOB, "/");
String day = "00", month = "00", year = "0000";
if (st.hasMoreTokens()) {
day = st.nextToken();
int monthint= Integer.parseInt(day);
if (monthint > 0 && monthint < 10) {
StringBuilder sb = new StringBuilder();
sb.append("0" + monthint);
day = sb.toString();
}
}
if (st.hasMoreTokens()) {
month = st.nextToken();
int monthint = Integer.parseInt(month);
if (monthint > 0 && monthint < 10) {
StringBuilder sb = new StringBuilder();
sb.append("0" + monthint);
month = sb.toString();
}
}
if (st.hasMoreTokens()) {
year = st.nextToken();
}
DOB = year+ "-" + month + "-" + day ;
RequestPinCode obj = new RequestPinCode();
if (!CheckConnection.checkStatus(this)) {
new DialogBox(this, "No network found.", "", "Alert!");
return;
}
String res = obj.execute(username, password, mobile, DOB).get();
if (!res.isEmpty()) {
if (res.equals("Data Not Valid")) {
new DialogBox(this, "The data you entered in not valid.", "", "Alert!");
}
if (res.equals("true")) {
new DialogBox(this, "Your pincode has been reset. Kindly view your email for details.",
"", "Alert!");
}
}
}
第一个if()条件中的“int monthint=Integer.parseInt(day);”行出现错误。我见过其他亲戚,但找不到解决我问题的合适人选。我将感谢任何帮助。我在stack over flow尝试了所有可能的方法,但还没有成功 如果要从字符串解析
日期
,请使用。
像这样使用它:
SimpleDateFormat fmt = new SimpleDateFormat("dd-MM-yyyy");
Date date = fmt.parse(dateString);
解决办法是什么?我得把约会安排得很简单。替换
StringTokenizer st=新的StringTokenizer(DOB,“/”)代码>使用此行StringTokenizer st=新的StringTokenizer(DOB.trim(),“-”)代码>
SimpleDateFormat fmt = new SimpleDateFormat("dd-MM-yyyy");
Date date = fmt.parse(dateString);