我試圖玩這個服務。現在,服務正在運行,它在任務欄上添加通知。當任務欄被點擊時,這個通知應該調用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
該錯誤消息說,你在你的活動有一個NullPointerException,你從服務發佈的代碼。我不驚訝。 – 2011-04-29 18:05:46