2017-04-14 21 views
0

我想將我的MainActivity.class的意圖設置爲與我的Notification.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用於包和類,因爲使用上下文的這個不起作用 –

+0

使用此方法時遇到了什麼問題? –

+0

getContext()給了我錯誤,所以我使用getBaseContext(),它給了我(java.lang.NullPointerException:嘗試調用null對象引用上的虛擬方法'java.lang.String android.content.Context.getPackageName()')你在這條線上的錯誤建議 –