내 Notification.class와 다른 패키지에있는 MainActivity.class에 인 텐트를 설정하고 싶습니다. ComponentName을 사용하려고했지만 addParentStack (componentName) 및 NotificationManager 행에 대해 NULL 포인터 예외를 여전히 제공합니다. java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
다른 패키지에서 알림 활동 시작
package com.workdemos.preference;
public class Notification extends AppCompatActivity {
public void pushNotification() {
ComponentName componentName = new ComponentName("com.workdemos.user", "MainActivity");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("New News")
.setContentText("new news found in your preferred city");
Intent resultIntent = new Intent();
resultIntent.setComponent(componentName);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(componentName);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}
UPDATE는
나는 MainActivity.class의 동일한 패키지에있는 호출자 클래스에서 컨텍스트를 전송했습니다.
public void pushNotification(Context context) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("New News")
.setContentText("new news found in your preferred city");
Intent resultIntent = new Intent();
resultIntent.setComponent(new ComponentName(context, MainActivity.class));
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getBaseContext());
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
이렇게하면 이전에 오류가 발생했습니다. 그러나 addParentStack (MainActivity.class)은 전달하지 않았습니다. 그 줄에서 여전히 나에게 같은 오류를 준다.
'ComponentName ("com.workdemos.user", "com.workdemos.user.MainActivity");와 같이 변경하십시오. MainActivity 앞에 전체 경로를 추가하십시오. –
나는 그랬다. 여전히 동일합니다. –
''com.workdemos.user ''는 (는) 당신의 응용 프로그램 패키지 이름입니까? –