android - How a Service can call an Activity? Official example does not work! -


i tried play service. now, service running, ... adds notification on task bar. when task bar clicked, notification should call myactivity.class. gives error. please see code, after error, error:

... w/dalvikvm( 2384): threadid=1: thread exiting uncaught exception (group=0x4001d7e0) e/androidruntime( 2384): fatal exception: main e/androidruntime( 2384): java.lang.runtimeexception: unable start activity componentinfo{activity.change/activity.change.myactivity}: java.lang.nullpointerexception

....

code:

public void addnotification()     {      string ns = context.notification_service;     notificationmanager mnotificationmanager = (notificationmanager) getsystemservice(ns);       int icon = r.drawable.icon; //.drawable.notification_icon;     charsequence tickertext = "hello";     long when = system.currenttimemillis();      notification notification = new notification(icon, tickertext, when);     //notification.defaults=notification.flag_only_alert_once+notification.flag_auto_cancel;     //notification.defaults=notification.flags     notification.flags|=notification.flag_auto_cancel;       context context = getapplicationcontext();     charsequence contenttitle = "uhs next";     charsequence contenttext = "hello world!";     intent notificationintent = new intent(this, class_b.class); // want call class_b, not a. has called "this" service though.     pendingintent contentintent = pendingintent.getactivity(this, 0, notificationintent, 0);      /*      * pendingintent malarmsender = pendingintent.getservice(alarmservice.this, 0, new intent(alarmservice.this, alarmservice_service.class), 0);      * */      notification.setlatesteventinfo(context, contenttitle, contenttext, contentintent);       //private static final int hello_id = 1;  << moved global declaration      mnotificationmanager.notify(hello_id, notification);  } 

// part of error log:

i/windowmanager(   90): ignoring home; event canceled. d/myservice( 5206): received message!! d/dalvikvm(   90): gc_external_alloc freed 22585 objects / 1120256 bytes in 79ms d/dalvikvm( 5206): gc_explicit freed 3702 objects / 224088 bytes in 77ms i/activitymanager(   90): starting activity: intent { act=android.intent.action.main      cat=[android.intent.category.home] flg=0x10200000      cmp=com.android.launcher/com.android.launcher2.launcher } d/dalvikvm(  172): gc_explicit freed 36 objects / 1944 bytes in 91ms i/activitymanager(   90): starting activity:      intent { cmp=activity.change/.w2pexampleandnew bnds=[0,387][480,483] } w/activitymanager(   90): startactivity called non-activity context;      forcing intent.flag_activity_new_task for:      intent { cmp=activity.change/.w2pexampleandnew bnds=[0,387][480,483] } v/renderscript_jni(  171): surfacedestroyed d/androidruntime( 5206): shutting down vm w/dalvikvm( 5206): threadid=1: thread exiting uncaught exception (group=0x4001d7e0) e/androidruntime( 5206): fatal exception: main e/androidruntime( 5206): java.lang.runtimeexception: unable start activity componentinfo     {activity.change/activity.change.w2pexampleandnew}: java.lang.nullpointerexception e/androidruntime( 5206):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2663) e/androidruntime( 5206):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2679) e/androidruntime( 5206):    @ android.app.activitythread.access$2300(activitythread.java:125) e/androidruntime( 5206):    @ android.app.activitythread$h.handlemessage(activitythread.java:2033) e/androidruntime( 5206):    @ android.os.handler.dispatchmessage(handler.java:99) e/androidruntime( 5206):    @ android.os.looper.loop(looper.java:123) e/androidruntime( 5206):    @ android.app.activitythread.main(activitythread.java:4627) e/androidruntime( 5206):    @ java.lang.reflect.method.invokenative(native method) e/androidruntime( 5206):    @ java.lang.reflect.method.invoke(method.java:521) e/androidruntime( 5206):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:858) e/androidruntime( 5206):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) e/androidruntime( 5206):    @ dalvik.system.nativestart.main(native method) e/androidruntime( 5206): caused by: java.lang.nullpointerexception e/androidruntime( 5206):    @ activity.change.w2pexampleandnew.oncreate(w2pexampleandnew.java:63) e/androidruntime( 5206):    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) e/androidruntime( 5206):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2627) e/androidruntime( 5206):    ... 11 more w/activitymanager(   90):   force finishing activity activity.change/.w2pexampleandnew w/activitymanager(   90): activity pause timeout historyrecord{44cafc20 activity.change/.w2pexampleandnew} v/renderscript_jni(  171): surfacecreated v/renderscript_jni(  171): surfacechanged d/wifiservice(   90): acquirewifilocklocked: wifilock{networklocationprovider type=2 binder=android.os.binder@44d28380} d/locationmasfclient(   90): getnetworklocation(): returning cache location accuracy 70.0 d/dalvikvm(  465): gc_for_malloc freed 6095 objects / 509112 bytes in 38ms w/activitymanager(   90): activity destroy timeout historyrecord{44cafc20 activity.change/.w2pexampleandnew} d/wifiservice(   90): releasewifilocklocked: wifilock{networklocationprovider type=2 binder=android.os.binder@44d28380} i/process ( 5206): sending signal. pid: 5206 sig: 9 w/inputmanagerservice(   90): window focused, ignoring focus gain of:      com.android.internal.view.iinputmethodclient$stub$proxy@44d18b80 i/activitymanager(   90): process activity.change (pid 5206) has died. w/activitymanager(   90): scheduling restart of crashed service activity.change/.w2pandroidservice in 5000ms i/activitymanager(   90): start proc activity.change service activity.change/.w2pandroidservice: pid=5258 uid=10073 gids={1006, 1015, 3003} d/myservice( 5258): oncreate 

make sure, have myactivity activity declared in androidmanifest.xml.

update
if myactivity main launcher activity (which starts service), , want start activity_b notification, both of these activities must declared in androidmanifest.xml (watch launchmode, if set any!):

<activity android:name="myactivity" android:label="@string/app_name">     <intent-filter>         <action android:name="android.intent.action.main" />         <category android:name="android.intent.category.launcher" />     </intent-filter> </activity> <activity android:name="activity_b" /> 

also, intent when specifying notification should like:

intent notificationintent = new intent(this, activity_b.class); 

depr.
try setting getapplicationcontext() instead of this when creating pendingintent:

pendingintent contentintent = pendingintent.getactivity(     getapplicationcontext(), 0, notificationintent, 0); 

Comments