Android notifications and vibration from the service

I have a service that connects to a server using AsyncTask. After parsing the response, I need to make a notification and vibrate, but get a NullpointerException.

@Override protected void onPostExecute(String[] result) { ReconnectCallback(result); super.onPostExecute(result); } public void ReconnectCallback(String[] params) { Handler mHandler = new Handler(); Notification notif = new Notification(R.drawable.ic_launcher, "Connection error", System.currentTimeMillis()); Intent x = new Intent(); PendingIntent pIntent = PendingIntent.getActivity(MyApplication.getAppContext() , 0, new Intent(), 0); notif.flags |= Notification.FLAG_AUTO_CANCEL; v.vibrate(new long[] { 300, 300 }, -1); notif.setLatestEventInfo(MyApplication.getAppContext(), "Taxi driver error!", "Err: parse exception", pIntent); nm.notify(9999, notif); 

Magazine

 11-05 12:22:12.829: E/AndroidRuntime(13698): FATAL EXCEPTION: main 11-05 12:22:12.829: E/AndroidRuntime(13698): java.lang.NullPointerException 11-05 12:22:12.829: E/AndroidRuntime(13698): at com.pkg.ReconnectCallback(NetworkService.java:240) 11-05 12:22:12.829: E/AndroidRuntime(13698): at com.pkg.NetworkService$startReconnection.onPostExecute(NetworkService.java:185) 11-05 12:22:12.829: E/AndroidRuntime(13698): at com.pkg.NetworkService$startReconnection.onPostExecute(NetworkService.java:1) 11-05 12:22:12.829: E/AndroidRuntime(13698): at android.os.AsyncTask.finish(AsyncTask.java:417) 11-05 12:22:12.829: E/AndroidRuntime(13698): at android.os.AsyncTask.access$300(AsyncTask.java:127) 11-05 12:22:12.829: E/AndroidRuntime(13698): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 11-05 12:22:12.829: E/AndroidRuntime(13698): at android.os.Handler.dispatchMessage(Handler.java:99) 11-05 12:22:12.829: E/AndroidRuntime(13698): at android.os.Looper.loop(Looper.java:130) 11-05 12:22:12.829: E/AndroidRuntime(13698): at android.app.ActivityThread.main(ActivityThread.java:3835) 11-05 12:22:12.829: E/AndroidRuntime(13698): at java.lang.reflect.Method.invokeNative(Native Method) 11-05 12:22:12.829: E/AndroidRuntime(13698): at java.lang.reflect.Method.invoke(Method.java:507) 11-05 12:22:12.829: E/AndroidRuntime(13698): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 11-05 12:22:12.829: E/AndroidRuntime(13698): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 11-05 12:22:12.829: E/AndroidRuntime(13698): at dalvik.system.NativeStart.main(Native Method) v.vibrate(new long[] { 300, 300 }, -1); nm.notify(9999, notif); 

causes NPE. What's wrong?

 @Override public void onCreate() { // TODO Auto-generated method stub nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); v = (Vibrator) getSystemService(VIBRATOR_SERVICE); super.onCreate(); } 
0
source share
1 answer

v and / or nm apparently null . Of course, one of these two is null if you should believe the stack trace.

+1
source

All Articles