2017-04-14 2 views
0

내 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)은 전달하지 않았습니다. 그 줄에서 여전히 나에게 같은 오류를 준다.

+0

'ComponentName ("com.workdemos.user", "com.workdemos.user.MainActivity");와 같이 변경하십시오. MainActivity 앞에 전체 경로를 추가하십시오. –

+0

나는 그랬다. 여전히 동일합니다. –

+0

''com.workdemos.user ''는 (는) 당신의 응용 프로그램 패키지 이름입니까? –

답변

0

인스턴스 시작 줄에서 전달 된 컨텍스트를 설정하는 생성자를 만들어이 작업을 수행하는 방법을 발견했습니다.

public class Notification { 

    private Context context; 

    public Notification(Context context) { 
     this.context = context; 
    } 

    public void pushNotification() { 
     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(context); 
     stackBuilder.addParentStack(new ComponentName(context, MainActivity.class)); 
     stackBuilder.addNextIntent(resultIntent); 

     PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
     builder.setContentIntent(resultPendingIntent); 

     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, builder.build()); 
    } 

} 
0

ComponentName에는 컨텍스트와 클래스를 매개 변수로 사용하는 생성자가 있습니다. 이를 사용하여 ComponentName을 가져올 수 있습니다.

ComponentName componentName = new ComponentName(getContext(), MainActivity.class); 
+0

컨텍스트를 사용하는이 클래스가 작동하지 않아서 ComponentName을 package 및 class로 사용했습니다. –

+0

이 접근법을 사용하는 동안 발생한 문제점은 무엇입니까? –

+0

getContext()가 나에게 오류를 주었으므로 나에게 준 getBaseContext()를 사용했다. (java.lang.NullPointerException : 널 객체 참조에서 가상 메소드 'java.lang.String android.content.Context.getPackageName()'을 호출하려고 시도 함) 제안한이 줄의 오류 –