2013-10-29 85 views
0

我做的應用程序。 當我按下切換按鈕,那麼它將設置警報。 然後,當我再次按下撥動按鈕1時,它將取消報警。 另外,如果我重新啓動應用程序,它不會改變我在重新啓動應用程序之前設置。Android部隊關閉,原因是什麼?

但是問題在於,它在我開始使用時非常有效,但是當我重新啓動它以取消警報時,它就會關閉。

so我該怎麼做才能使它沒有錯誤?

這裏是代碼。

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

logcat的


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 
+2

你能後的堆棧跟蹤?使用這個堆棧跟蹤,你可以確定它在哪一行失敗。確保你在你的問題中包含了這一行。 – doydoy

+0

什麼是堆棧跟蹤?你能解釋一下嗎?那我該如何使用它? – user2899769

+0

當您運行應用程序時,您會在logcat窗口中看到輸出。您可以通過您的應用程序特有的內容來對其進行過濾,例如程序包名稱的一部分,以便它能夠顯示您感興趣的內容。編號建議google logog – doydoy

回答

0

讀取日誌,它說:

顯示java.lang.NullPointerException在 com.simpson.knflagralarm.MainActivity.onToggleClicked( MainActivity.java:226)

這是你的問題

+0

然後,我該如何解決這個問題? – user2899769

+0

@ user2899769你沒有向我們展示行號,所以我不知道。看看第226行,並試圖理解那裏是什麼,然後你就可以修復它。 ' –

+0

'alarm.cancel(pendingIntent0606());'是226行的代碼。 – user2899769

相關問題