2013-11-01 6 views
0

У меня есть работающий фон и проверяется каждый день с помощью протокола BroadcastReceiver и AlarmManager, и если какие-либо изменения происходят, сообщите об этом пользователю. Таким образом, в основном в службе я запускаю класс асинтетики и проверяю URL-адрес URL-адресов и уведомляю пользователя.Уведомление Android от службы по классу AsyncTask

Вот код:

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) 

Может ли кто-нибудь помочь мне решить эту проблему. Благодаря

+0

что линия 147? 'notificationManager2' имеет значение null? – Raghunandan

+0

Строка 147 - уведомлениеManager2.notify (notificationIdentifier, notification2); –

+0

'NullPointerException' является самым простым исключением. Вы можете узнать причину. Просто отлаживайте строки за строкой. «Прогресс» может быть пустым, и вы вызываете «прогресс [0]» .... –

ответ

0

Используйте ниже

NotificationManager notificationManager2 = 
(NotificationManager) servicecontext.getSystemService(android.content.Context.NOTIFICATION_SERVICE); 
+0

хорошо, спасибо, что это решение. –

Смежные вопросы