2013-03-01 47 views
0

我正在Android中進行項目。當通知進入活動時被迫關閉

當我在活動(groups.class)我打電話給這個函數(活動內聲明):

public void lookForGroups() 
    { 
     int seconds = 20; 

      Intent myIntent = new Intent(Groups.this, GroupsTaskAlarmChecker.class); 
      pendingIntent = PendingIntent.getService(Groups.this, 0, myIntent, 0); 

      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 

      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      calendar.add(Calendar.SECOND, 10); 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), seconds * 1000, pendingIntent); 

    } 

我在連接到網絡PHP和收到回覆裏面的服務和然後我創建一個Notification(在Service中聲明)發送一個消息給一個調用函數notifyNew()的處理函數。

public class GroupsTaskAlarmChecker extends Service implements Runnable { 
     public void run() { php call and send msg to handler to call notify function} 
     private void notifyNew() { 
     Intent intentNot = new Intent(this, Groups.class); 

     Notification notification = new Notification(R.drawable.icon, "changes", System.currentTimeMillis()); 
     notification.setLatestEventInfo(this, getString(R.string.app_name), getString(R.string.notifiedGroup), 
       PendingIntent.getActivity(this, 0, intentNot, PendingIntent.FLAG_CANCEL_CURRENT)); 

     notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.defaults |= Notification.DEFAULT_VIBRATE; 

     notification.defaults |= Notification.DEFAULT_LIGHTS; 
     notification.flags = Notification.FLAG_AUTO_CANCEL; 

     try 
     { 
      notification.ledARGB = 0xff00ff00; 
      notification.ledOnMS = 300; 
      notification.ledOffMS = 1000; 
      notification.flags |= Notification.FLAG_SHOW_LIGHTS; 
     }catch(Exception ex) 
     {   
     }   
     manager.notify(APP_ID_NOTIFICATION, notification); 
} 
} 

我想再次觸摸通知時再次進入活動「組」。 所有這一切工作正常,但是當我觸摸通知logcat的顯示...

03-01 17:52:50.663: E/AndroidRuntime(17019): FATAL EXCEPTION: main 
03-01 17:52:50.663: E/AndroidRuntime(17019): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.org.tfc_android/com.org.tfc_android.Groups}: java.lang.NullPointerException 
03-01 17:52:50.663: E/AndroidRuntime(17019): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at android.app.ActivityThread.access$600(ActivityThread.java:150) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at android.os.Looper.loop(Looper.java:137) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at android.app.ActivityThread.main(ActivityThread.java:5193) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at java.lang.reflect.Method.invokeNative(Native Method) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at java.lang.reflect.Method.invoke(Method.java:511) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at dalvik.system.NativeStart.main(Native Method) 
03-01 17:52:50.663: E/AndroidRuntime(17019): Caused by: java.lang.NullPointerException 
03-01 17:52:50.663: E/AndroidRuntime(17019): at com.org.tfc_android.Groups.onCreate(Groups.java:61) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at android.app.Activity.performCreate(Activity.java:5104) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
03-01 17:52:50.663: E/AndroidRuntime(17019): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258) 
03-01 17:52:50.663: E/AndroidRuntime(17019): ... 11 more 

有強制關閉,但後來去正確的活動團體 有人能幫助我嗎?

求解: 求解add intentNot.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);當在通知之前創建Intent時。

+0

您是否在意圖中添加了任何標誌。它是什麼類型的活動(例如singleTon)? – 2013-03-01 17:32:38

+0

您是否嘗試過在新的上下文行中使用getApplicationcontext()?我有時覺得這有幫助。 – 2013-03-01 17:44:18

+0

@KnightTemplar從你的堆棧跟蹤:「at com.org.tfc_android.Groups.onCreate(Groups.java:61)」你能說Groups.java的第61行是什麼嗎? – 2013-03-01 17:44:23

回答

1

我解決了add intentNot.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);感謝@FahadIshaque