【问题标题】:Null error when I try to notify something当我尝试通知某事时出现空错误
【发布时间】:2014-10-31 19:40:44
【问题描述】:

大家好,我刚开始使用 Android,我正在尝试从与我的主要活动不同的类发出通知。但是 e.printStackTrace() 说“null”并停在以下行:“NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);”如果我从 mainActivity 发出相同的通知,一切顺利。你能帮我吗?

if(giorni_di_differenza <= 15)
{

    try{
        PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
        NotificationCompat.Builder n = new NotificationCompat.Builder(context)
            .setContentTitle(nome_evento)
            .setContentText(descrizione_evento)
            .setContentIntent(pi)
            .setAutoCancel(true)
            .setLights(Color.GREEN, 1000, 1000)
            .setSmallIcon(R.drawable.ic_launcher);

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(0, n.build());

    }catch(Exception e){
        e.printStackTrace();
    }
}

如果您需要更多代码,我可以发给您。

LogCat:http://pastebin.com/W4hKbf6W(Pause GC错误是三星库存ROM的错误)

【问题讨论】:

  • 好的,一秒钟 @kalyanpvs

标签: android notifications


【解决方案1】:

您需要Context 才能从外部Activity 访问NotificationManager

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

而且你的logcat也清楚地说

java.lang.IllegalStateException:系统服务不可用于 onCreate()之前的活动

只有在onCreate()Activity 之后才能访问NotificationManager

【讨论】:

  • 它可以工作,但是,当调试停止时,通知消失:0
  • 它工作正常,问题是我做了两个通知,所以......我怎样才能做到这两个? @MD
  • @PierpaoloErcoli 我没明白。你在谈论什么?
  • 如果我调用两个不同的通知,例如一个通知 GPS 已关闭,另一个通知其他任何内容。我的应用通知第二个。
  • @PierpaoloErcoli 为此,您必须将notificationManager.notify(0, n.build());NotificationID 更改为01
【解决方案2】:

是的,因为它在您的课程中找不到上下文:

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

这样做:

//pass different id for different notifications
private void showNotification(Context con, int notificationID) {
    if(giorni_di_differenza <= 15)
    {

        try{
            PendingIntent pi = PendingIntent.getActivity(con, 0, new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
            NotificationCompat.Builder n = new NotificationCompat.Builder(con)
                .setContentTitle(nome_evento)
                .setContentText(descrizione_evento)
                .setContentIntent(pi)
                .setAutoCancel(true)
                .setLights(Color.GREEN, 1000, 1000)
                .setSmallIcon(R.drawable.ic_launcher);
        NotificationManager notificationManager = (NotificationManager) con.getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(notificationID, n.build());

    }catch(Exception e){
        e.printStackTrace();
    }
}
}

【讨论】:

  • 它工作正常,问题是我做了两个通知,所以......我怎样才能做到这两个? @哈马德
  • 改变这个:notificationManager.notify(0, n.build());到这个 notificationManager.notify(1, n.build());
  • 等等我还有一个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-04
  • 2023-04-08
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 2020-06-17
  • 1970-01-01
相关资源
最近更新 更多