Tuesday, April 9, 2013

[android help] Can't to use add(Calendar.DAY_OF_MONTH,1) in android


i tried to use button onClick handler to add 1 day to calendar, but it throws error, when i tap to nextDay button. That's a code fragment:



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));
}
}


I have also tried , doesn't work. this.c.add(Calendar.DAY_OF_MONTH, 1);


LogCat log:



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)


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...