我想在应用启动service后产生一个notification,在通知栏点击notification后跳转到另一个程序,代码如下
public class TuiService extends Service {
public TuiService() {
Intent intent = new Intent();
ComponentName cn = new ComponentName("com.example.myarcbar","com.example.myarcbar.MainActivity");
intent.setComponent(cn);
PendingIntent pendingIntent = PendingIntent.getActivity(TuiService.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(TuiService.this)
.setSmallIcon(R.drawable.nnn)
.setAutoCancel(true)
.setContentTitle("NotificationTest")
.setContentText("success")
.setContentIntent(pendingIntent);
notificationManager.notify(1, builder.build());
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这段代码在activity中使用正常,可把它放到service中报错java.lang.RuntimeException: Unable to instantiate service com.example.notitest.TuiService: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
错误来自PendingIntent pendingIntent = PendingIntent.getActivity(TuiService.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);这一行,求解答。。。