0
我有一個服務運行後臺並使用BroadcastReceiver和AlarmManager檢查每天的網頁排名,如果發生任何更改通知用戶。所以基本上在一個服務我運行asynctask類和檢查網址pagerank並給用戶通知。來自AsyncTask類的服務的Android通知
下面是代碼:
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)
任何人都可以幫助我解決這個錯誤。 感謝
是什麼線147? 'notificationManager2'爲空? – Raghunandan
147行是notificationManager2.notify(notificationIdentifier,notification2); –
'NullPointerException'是最簡單的異常,你可以找出原因。只需逐行調試即可。 'progress'可能爲空,您可以調用'progress [0]'... –