我制作了应用程序。当我按下切换按钮时,它将设置警报。然后,当我再按一次切换按钮 1 时,它将取消警报。另外,如果我重新启动应用程序,在重新启动应用程序之前设置的内容并没有改变。
但是问题来了,当我启动它时效果很好,但是当我重新启动它以取消警报时,它是强制关闭的。
所以我应该怎么做才能使它没有错误?
这是代码。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences pref = getSharedPreferences("pref", 0);
ToggleButton tglbutton = (ToggleButton)findViewById(R.id.toggleButton1);
TextView text1 = (TextView) findViewById(R.id.View1);
Boolean tgl = pref.getBoolean("toggleButton", false);
String textData = pref.getString("text1", "");
tglbutton.setChecked(tgl);
text1.setText(textData);
}
public void onToggleClicked(View view) {
boolean on = ((ToggleButton) view).isChecked();
if(on){
alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
{
calendar0606.set(Calendar.MONTH, Calendar.JUNE);
calendar0606.set(Calendar.DAY_OF_MONTH, 6);
int month0606 = calendar0606.get(Calendar.MONTH) + 1;
//month2= MARCH
int day0606 = calendar0606.get(Calendar.DAY_OF_MONTH);
if(month > month0606){
int year = calendar0606.get(Calendar.YEAR)+1;
calendar0606.set(Calendar.YEAR, year);
}
else if(month <= month0606)
{if(day >day0606){
int year = calendar0606.get(Calendar.YEAR)+1;
calendar0606.set(Calendar.YEAR, year);
}}
}
TextView text1 = (TextView) findViewById(R.id.View1);
text1.setText(R.string.working);}else{
alarm.cancel(pendingIntent0606());
return;
}
setAlarm0606();}
public void onStop(){
super.onStop();
SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
ToggleButton tglbutton = (ToggleButton)findViewById(R.id.toggleButton1);
TextView text1 = (TextView) findViewById(R.id.View1);
editor.putBoolean("toggleButton", tglbutton.isChecked());
editor.putString("text1", text1.getText().toString());
editor.commit();
}
日志猫
03-01 22:37:59.207: D/szipinf(7912): Initializing inflate state
03-01 22:37:59.207: D/szipinf(7912): Initializing zlib to inflate
03-01 22:37:59.207: D/szipinf(7912): Initializing inflate state
03-01 22:37:59.207: D/szipinf(7912): Initializing zlib to inflate
03-01 22:37:59.207: D/szipinf(7912): Initializing inflate state
03-01 22:37:59.207: D/szipinf(7912): Initializing zlib to inflate
03-01 22:37:59.207: D/szipinf(7912): Initializing inflate state
03-01 22:37:59.207: D/szipinf(7912): Initializing zlib to inflate
03-01 22:37:59.207: D/szipinf(7912): Initializing inflate state
03-01 22:37:59.207: D/szipinf(7912): Initializing zlib to inflate
03-01 22:37:59.207: D/szipinf(7912): Initializing inflate state
03-01 22:37:59.207: D/szipinf(7912): Initializing zlib to inflate
03-01 22:37:59.207: D/szipinf(7912): Initializing inflate state
03-01 22:37:59.207: D/szipinf(7912): Initializing zlib to inflate
03-01 22:37:59.257: D/dalvikvm(7912): GC_EXTERNAL_ALLOC freed 52K, 49% free 2783K/5379K, external 1973K/2108K, paused 31ms
03-01 22:39:23.089: D/dalvikvm(7912): Debugger has detached; object registry had 1 entries
03-01 22:41:02.128: I/dalvikvm(8188): Total arena pages for JIT: 11
03-01 22:41:12.167: D/AndroidRuntime(8188): Shutting down VM
03-01 22:41:12.167: W/dalvikvm(8188): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
03-01 22:41:12.210: E/AndroidRuntime(8188): FATAL EXCEPTION: main
03-01 22:41:12.210: E/AndroidRuntime(8188): java.lang.IllegalStateException: Could not execute method of the activity
03-01 22:41:12.210: E/AndroidRuntime(8188): at android.view.View$1.onClick(View.java:2154)
03-01 22:41:12.210: E/AndroidRuntime(8188): at android.view.View.performClick(View.java:2538)
03-01 22:41:12.210: E/AndroidRuntime(8188): at android.widget.CompoundButton.performClick(CompoundButton.java:99)
03-01 22:41:12.210: E/AndroidRuntime(8188): at android.view.View$PerformClick.run(View.java:9152)
03-01 22:41:12.210: E/AndroidRuntime(8188): at android.os.Handler.handleCallback(Handler.java:587)
03-01 22:41:12.210: E/AndroidRuntime(8188): at android.os.Handler.dispatchMessage(Handler.java:92)
03-01 22:41:12.210: E/AndroidRuntime(8188): at android.os.Looper.loop(Looper.java:130)
03-01 22:41:12.210: E/AndroidRuntime(8188): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-01 22:41:12.210: E/AndroidRuntime(8188): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 22:41:12.210: E/AndroidRuntime(8188): at java.lang.reflect.Method.invoke(Method.java:507)
03-01 22:41:12.210: E/AndroidRuntime(8188): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-01 22:41:12.210: E/AndroidRuntime(8188): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-01 22:41:12.210: E/AndroidRuntime(8188): at dalvik.system.NativeStart.main(Native Method)
03-01 22:41:12.210: E/AndroidRuntime(8188): Caused by: java.lang.reflect.InvocationTargetException
03-01 22:41:12.210: E/AndroidRuntime(8188): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 22:41:12.210: E/AndroidRuntime(8188): at java.lang.reflect.Method.invoke(Method.java:507)
03-01 22:41:12.210: E/AndroidRuntime(8188): at android.view.View$1.onClick(View.java:2149)
03-01 22:41:12.210: E/AndroidRuntime(8188): ... 12 more
03-01 22:41:12.210: E/AndroidRuntime(8188): Caused by: java.lang.NullPointerException
03-01 22:41:12.210: E/AndroidRuntime(8188): at com.simpson.knflagralarm.MainActivity.onToggleClicked(MainActivity.java:226)
03-01 22:41:12.210: E/AndroidRuntime(8188): ... 15 more