开发者

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

I tried to play with the Service. Now, a service is running, ... it adds notification on task bar. When task bar is clicked, this notification should call MyActivity.class. But it gives error. Please see the code, after the error, Error:

... W/dalvikvm( 2384): threadid=1: thread exiting with uncaught exception (group=0x4001d7e0) E/AndroidRuntime( 2384): FATAL EXCEPTION: main E/AndroidRuntime( 2384): java.lang.RuntimeException: Unable to 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); // I want to call class_B, not A. 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 to the 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 from 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 with uncaught excepti开发者_如何学Goon (group=0x4001d7e0)
E/AndroidRuntime( 5206): FATAL EXCEPTION: main
E/AndroidRuntime( 5206): java.lang.RuntimeException: Unable to start activity ComponentInfo
    {activity.change/activity.change.W2PExampleAndNew}: java.lang.NullPointerException
E/AndroidRuntime( 5206):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime( 5206):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime( 5206):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime( 5206):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime( 5206):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 5206):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 5206):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 5206):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 5206):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 5206):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
E/AndroidRuntime( 5206):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
E/AndroidRuntime( 5206):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 5206): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 5206):    at activity.change.W2PExampleAndNew.onCreate(W2PExampleAndNew.java:63)
E/AndroidRuntime( 5206):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 5206):    at 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 for 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 with accuracy 70.0
D/dalvikvm(  465): GC_FOR_MALLOC freed 6095 objects / 509112 bytes in 38ms
W/ActivityManager(   90): Activity destroy timeout for 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 already 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 for service activity.change/.W2PAndroidService: pid=5258 uid=10073 gids={1006, 1015, 3003}
D/MyService( 5258): onCreate


Make sure, that you have the MyActivity activity declared in your androidManifest.xml.

Update
If MyActivity is your main launcher activity (which starts the service), and you want to start Activity_B from your notification, then both of these activities must be declared in your androidManifest.xml (watch for the launchMode, if you 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, the Intent when specifying your notification should look like:

Intent notificationIntent = new Intent(this, Activity_B.class);

Depr.
You could also try setting getApplicationContext() instead of this when creating the PendingIntent:

PendingIntent contentIntent = PendingIntent.getActivity(
    getApplicationContext(), 0, notificationIntent, 0);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜