0

我有一个运行后台的服务,并使用 BroadcastReceiver 和 AlarmManager 检查 URL 的日常 pagerank,如果发生任何更改,请通知用户。所以基本上在服务中我正在运行 asynctask 类并检查 urls pagerank 并向用户发出通知。

这是代码:

public class PagerankCheck extends AsyncTask<String, Integer, Integer>{
    private android.content.Context servicecontext;
    private String uri = "";

 public PagerankCheck(android.content.Context context, String url) {
        servicecontext = context;
        uri = url;
    }

protected void onProgressUpdate(Integer... progress) {
    int icon = R.drawable.ic_launcher;
    CharSequence ticker = uri + " pagerank changed from " + oldPR + " to " + progress[0].toString();
    long showAt = System.currentTimeMillis(); 
    CharSequence notificationTitle = "Notification:";
    CharSequence notificationMessage = "Test.";
    final int notificationIdentifier = 101;
    NotificationManager notificationManager2 =
    (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
    Notification notification2 = new Notification(icon, ticker, showAt);
    android.content.Intent intent = new android.content.Intent();
    PendingIntent pendingIntent2 = PendingIntent.getActivity(servicecontext, 0, intent, 0);
    notification2.setLatestEventInfo(servicecontext,  notificationTitle, notificationMessage, pendingIntent2);
    notificationManager2.notify(notificationIdentifier, notification2);
}

在执行最后一行代码时,LogCat 出现以下错误。

11-01 07:14:26.488: E/AndroidRuntime(851): FATAL EXCEPTION: main
11-01 07:14:26.488: E/AndroidRuntime(851): java.lang.NullPointerException
11-01 07:14:26.488: E/AndroidRuntime(851):  at org.siteprice.googlepagerankchecker.PagerankCheck.onProgressUpdate(PagerankCheck.java:147)
11-01 07:14:26.488: E/AndroidRuntime(851):  at org.siteprice.googlepagerankchecker.PagerankCheck.onProgressUpdate(PagerankCheck.java:1)
11-01 07:14:26.488: E/AndroidRuntime(851):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
11-01 07:14:26.488: E/AndroidRuntime(851):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 07:14:26.488: E/AndroidRuntime(851):  at android.os.Looper.loop(Looper.java:137)
11-01 07:14:26.488: E/AndroidRuntime(851):  at android.app.ActivityThread.main(ActivityThread.java:5103)
11-01 07:14:26.488: E/AndroidRuntime(851):  at java.lang.reflect.Method.invokeNative(Native Method)
11-01 07:14:26.488: E/AndroidRuntime(851):  at java.lang.reflect.Method.invoke(Method.java:525)
11-01 07:14:26.488: E/AndroidRuntime(851):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-01 07:14:26.488: E/AndroidRuntime(851):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-01 07:14:26.488: E/AndroidRuntime(851):  at dalvik.system.NativeStart.main(Native Method)

谁能帮我解决这个错误。谢谢

4

1 回答 1

0

使用以下

NotificationManager notificationManager2 =
(NotificationManager) servicecontext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
于 2013-11-01T11:24:15.267 回答