我正在开发和使用 Android Studio。我有一个使用日历从用户输入中获取日期的屏幕。一旦我将代码添加到这个问题上,我的移动到另一个屏幕的按钮已经停止工作。我花了几个小时在这上面,找不到我哪里出错了。我怀疑这与我的代码顺序有关,但在这里苦苦挣扎。
这是java类的代码
//class starts here
package com.MSL.claimssolutions1;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Calendar;
public class accident_details_activity extends Activity
{
//set variables
private int mYear;
private int mMonth;
private int mDay;
private TextView mDateDisplay;
private Button mPickDate;
static final int DATE_DIALOG_ID = 0;
accident_details_activity ob;
public void onCreate(Bundle icicle)
{
//listen for click from button to move to next screen
// this is where I think my code is going wrong - I think should be moved elsewhere, not sure though
super.onCreate(icicle);
setContentView(R.layout.accident_details_activity);
Button StepFive = (Button) findViewById(R.id.StepFive_button);
StepFive.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//move to next screen - i.e. upload files
Intent i = new Intent(accident_details_activity.this, upload_file_activity.class);
startActivity(i);
}
});
//add calendar functionality
//@Override
super.onCreate(icicle);
setContentView(R.layout.accident_details_activity);
mDateDisplay = (EditText) findViewById(R.id.AccidentDate);
mPickDate = (Button) findViewById(R.id.myDatePickerButton);
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date
updateDisplay();
}
public void setOb( accident_details_activity obA){
this.ob=obA;
}
//update text field with date selected from calendar
private void updateDisplay() {
this.mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("-")
.append(mMonth + 1).append("-")
.append(mYear).append(" "));
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
}