2011-04-29 97 views
0

我试图玩这个服务。现在,服务正在运行,它在任务栏上添加通知。当任务栏被点击时,这个通知应该调用MyActivity.class。 但它给错误。请查看代码,在错误发生后, 错误:服务如何调用活动?官方示例不起作用!

... 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

....

代码:

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 exception (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 [email protected]} 
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 [email protected]} 
I/Process (5206): Sending signal. PID: 5206 SIG: 9 
W/InputManagerService( 90): Window already focused, ignoring focus gain of: 
    [email protected] 
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 
+0

该错误消息说,你在你的活动有一个NullPointerException,你从服务发布的代码。我不惊讶。 – 2011-04-29 18:05:46

回答

0

确保,你有MyActivity活动在androidManifest.xml声明。

更新
如果MyActivity是你的主要发射活动(该活动启动服务),并且你想从您的通知开始Activity_B,那么这两项活动都必须在你的androidManifest.xml(手表宣布为launchMode,如果你设置任何):

<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" /> 

此外,Intent指定您的通知的时候应该是这样的:

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

Depr。
您也可以尝试创建PendingIntent时设置getApplicationContext()而不是this

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

谢谢!我尝试了两种方式,在通知和待处理内容中的“this”,以及getAppliocationContext,而不是“this”,但没有运气,说,“从非活动上下文中调用startactivity”:( – 2011-04-30 10:04:31

+0

)请分享您的'androidManifest.xml ''好吧,谢谢 – rekaszeru 2011-04-30 10:11:41

+0

@rekaszeru,非常感谢你,现在我可以告诉你我身边存在什么问题,对于启动服务的活动,它可以正常工作。 {调用Service_My_service} 然后,Service_My_service可以拨打Class_A当我点击该通知。 但是,其实我需要调用Class_B,不Class_A。 Class_A和Class_B是在同一个包! 有什么想法? 非常感谢您的时间! – 2011-04-30 10:31:59