我尝试使用按钮 onClick 处理程序将 1 天添加到日历,但是当我点击 nextDay 按钮时它会引发错误。这是一个代码片段:
public class Main extends Activity {
TextView dateView;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 1;
GregorianCalendar c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dateView = (TextView)findViewById(R.id.dateView);
GregorianCalendar c = new GregorianCalendar();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
public void nextDay(View v) {
c.add(Calendar.DAY_OF_MONTH, 1);
updateDateDisplay();
}
private void updateDateDisplay() {
String[] month_names = getResources().getStringArray(R.array.month_names);
dateView.setText(
new StringBuilder()
.append(mDay).append(" ")
.append(month_names[mMonth]).append(" ").append(mYear));
}
}
我也试过,不行。this.c.add(Calendar.DAY_OF_MONTH, 1);
LogCat 日志:
FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:2072)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)