我尝试使用该服务。现在,服务正在运行,...它在任务栏上添加了通知。单击任务栏时,此通知应调用MyActivity.class。 但它给出了错误。错误发生后请查看代码 错误:
... W / dalvikvm(2384):threadid = 1: 线程退出与未捕获的异常 (group = 0x4001d7e0)E / AndroidRuntime( 2384):致命的例外:主要 E / AndroidRuntime(2384): java.lang.RuntimeException:无法 开始活动 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 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
答案 0 :(得分: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。强>
您还可以在创建getApplicationContext()
时尝试设置this
而不是PendingIntent
:
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(), 0, notificationIntent, 0);