我正在开发和使用 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;
}
}
最佳答案
您正在调用 setContentView(),然后查找要单击的 View ,设置监听器,然后再次调用 setContentView()。这将清除您刚刚设置的 View 。更不用说对 StepFive 按钮的引用将不再位于同一层次结构中。
仅调用 setContentView 一次,然后绑定(bind)变量和监听器。
关于java - 移动到另一个屏幕不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20687479/