【问题标题】:forced close when notification goes to activity通知进入活动时强制关闭
【发布时间】:2013-03-01 17:29:40
【问题描述】:

我正在 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);

    }

在服务内部,我连接到 web 中的 php 并接收答案,然后创建一个通知(在服务内部声明),向调用函数 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

有一个强制关闭,但后来直接转到活动组 有人可以帮我吗?

已解决: 解决了添加 intentNot.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);在 Notification 之前创建 Intent 时。

【问题讨论】:

  • 您是否在意图中添加了任何标志。它是什么类型的活动(例如 singleTon)?
  • 您是否尝试过在新的 Context 行中使用 getApplicationcontext()?我有时会发现这会有所帮助。
  • @KnightTemplar 从你的堆栈跟踪:“在 com.org.tfc_android.Groups.onCreate(Groups.java:61)”你能说一下 Groups.java 第 61 行的内容吗?
  • 然后我解决了 add intentNot.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);感谢@FahadIshaque 提供线索。这是纠正该问题的最佳方法吗?谢谢
  • 欢迎。请也发布答案并接受它。这是很常见的问题。这可能会帮助许多其他人。谢谢

标签: android service android-activity notifications forceclose


【解决方案1】:

我解决了 addintentNot.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);感谢@FahadIshaque

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多