Android 推送通知到来时应用崩溃
Android app crashes when push notification is coming
我有一个用于地理围栏的广播接收器。完全相同的代码在另一个应用程序中工作,我在 Activity 中有一个完整的 Google 地图。在新的应用程序中,我希望 Google 地图(片段)与其他一些按钮和文本成为一个 activity.
的一部分
当我将它添加到 Manifest.xml 时(见下文),应用程序打开,当我收到通知时(我真的收到了我想要的通知)应用程序崩溃了。
<receiver
android:name=".GeofenceBroadcastReceiver"
android:enabled="true"
android:exported="true"></receiver>
没有这个 应用程序可以运行,但我没有收到通知。
所以在某些时候这部分很重要,但它可能会阻止其他内容?
这是 GeofenceBroadcastReceiver.java:(最后是 switch/case 我调用通知的地方)
public class GeofenceBroadcastReceiver extends BroadcastReceiver {
HauptActivity HA = new HauptActivity();
private static final String TAG = "GeofenceBroadcastReceiv";
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
// Toast.makeText(context, "Geofence triggered...", Toast.LENGTH_SHORT).show();
NotificationHelper notificationHelper = new NotificationHelper(context);
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()){
Log.d(TAG, "onReceive: Error receiving geofence event...");
return;
}
List<Geofence> geofenceList = geofencingEvent.getTriggeringGeofences();
for (Geofence geofence: geofenceList) {
Log.d(TAG, "onReceive: " + geofence.getRequestId());
}
// Location location = geofencingEvent.getTriggeringLocation();
int transitionType = geofencingEvent.getGeofenceTransition();
switch (transitionType) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
Toast.makeText(context, "GEOFENCE_TRANSITION_ENTER", Toast.LENGTH_SHORT).show();
notificationHelper.sendHighPriorityNotification("GEOFENCE_TRANSITION_ENTER", "", HauptActivity.class);
HA.changeMeldungGefahr();
break;
case Geofence.GEOFENCE_TRANSITION_DWELL:
Toast.makeText(context, "GEOFENCE_TRANSITION_DWELL", Toast.LENGTH_SHORT).show();
notificationHelper.sendHighPriorityNotification("GEOFENCE_TRANSITION_DWELL", "", HauptActivity.class);
break;
case Geofence.GEOFENCE_TRANSITION_EXIT:
Toast.makeText(context, "GEOFENCE_TRANSITION_EXIT", Toast.LENGTH_SHORT).show();
notificationHelper.sendHighPriorityNotification("GEOFENCE_TRANSITION_EXIT", "", HauptActivity.class);
break;
}
}
}
这是NotificationHelper.java(我从网上得到的代码)
public class NotificationHelper extends ContextWrapper {
private static final String TAG = "NotificationHelper";
public NotificationHelper(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannels();
}
}
private String CHANNEL_NAME = "High priority channel";
private String CHANNEL_ID = "com.example.notifications" + CHANNEL_NAME;
@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannels() {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("this is the description of the channel.");
notificationChannel.setLightColor(Color.RED);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
}
public void sendHighPriorityNotification(String title, String body, Class activityname) {
Intent intent = new Intent(this, activityname);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 267, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
// .setContentTitle(title)
// .setContentText(body)
.setSmallIcon(R.drawable.ic_launcher_background)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setStyle(new NotificationCompat.BigTextStyle().setSummaryText("Überwachungskamera").setBigContentTitle(title).bigText(body))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
NotificationManagerCompat.from(this).notify(new Random().nextInt(), notification);
}
}
它与我之前在旧应用程序中使用的代码完全相同,并且一切正常。所以我真的不想对代码做太多改动,因为它应该可以完美运行。也许我在别的地方忘了什么?也许问题不是代码...但我没有找到解决方案,也许你有。
编辑:这是“运行”下的红色文本
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mapbox1, PID: 32082
java.lang.RuntimeException: Unable to start receiver com.example.mapbox1.GeofenceBroadcastReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3399)
at android.app.ActivityThread.-wrap18(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1780)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:655)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
at com.example.mapbox1.HauptActivity.changeMeldungGefahr(HauptActivity.java:221)
at com.example.mapbox1.GeofenceBroadcastReceiver.onReceive(GeofenceBroadcastReceiver.java:47)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3392)
at android.app.ActivityThread.-wrap18(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1780)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
并说明一下:应用程序启动时,我会看到“HauptActivity”,然后我会根据需要收到通知。但是收到通知后应用程序立即崩溃。
来自日志:调用
HauptActivity.changeMeldungGefahr(HauptActivity.java:221)
导致空指针。
您应该修复 changeMeldungGefahr()
内的空指针。
我有一个用于地理围栏的广播接收器。完全相同的代码在另一个应用程序中工作,我在 Activity 中有一个完整的 Google 地图。在新的应用程序中,我希望 Google 地图(片段)与其他一些按钮和文本成为一个 activity.
的一部分当我将它添加到 Manifest.xml 时(见下文),应用程序打开,当我收到通知时(我真的收到了我想要的通知)应用程序崩溃了。
<receiver
android:name=".GeofenceBroadcastReceiver"
android:enabled="true"
android:exported="true"></receiver>
没有这个
所以在某些时候这部分很重要,但它可能会阻止其他内容?
这是 GeofenceBroadcastReceiver.java:(最后是 switch/case 我调用通知的地方)
public class GeofenceBroadcastReceiver extends BroadcastReceiver {
HauptActivity HA = new HauptActivity();
private static final String TAG = "GeofenceBroadcastReceiv";
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
// Toast.makeText(context, "Geofence triggered...", Toast.LENGTH_SHORT).show();
NotificationHelper notificationHelper = new NotificationHelper(context);
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()){
Log.d(TAG, "onReceive: Error receiving geofence event...");
return;
}
List<Geofence> geofenceList = geofencingEvent.getTriggeringGeofences();
for (Geofence geofence: geofenceList) {
Log.d(TAG, "onReceive: " + geofence.getRequestId());
}
// Location location = geofencingEvent.getTriggeringLocation();
int transitionType = geofencingEvent.getGeofenceTransition();
switch (transitionType) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
Toast.makeText(context, "GEOFENCE_TRANSITION_ENTER", Toast.LENGTH_SHORT).show();
notificationHelper.sendHighPriorityNotification("GEOFENCE_TRANSITION_ENTER", "", HauptActivity.class);
HA.changeMeldungGefahr();
break;
case Geofence.GEOFENCE_TRANSITION_DWELL:
Toast.makeText(context, "GEOFENCE_TRANSITION_DWELL", Toast.LENGTH_SHORT).show();
notificationHelper.sendHighPriorityNotification("GEOFENCE_TRANSITION_DWELL", "", HauptActivity.class);
break;
case Geofence.GEOFENCE_TRANSITION_EXIT:
Toast.makeText(context, "GEOFENCE_TRANSITION_EXIT", Toast.LENGTH_SHORT).show();
notificationHelper.sendHighPriorityNotification("GEOFENCE_TRANSITION_EXIT", "", HauptActivity.class);
break;
}
}
}
这是NotificationHelper.java(我从网上得到的代码)
public class NotificationHelper extends ContextWrapper {
private static final String TAG = "NotificationHelper";
public NotificationHelper(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannels();
}
}
private String CHANNEL_NAME = "High priority channel";
private String CHANNEL_ID = "com.example.notifications" + CHANNEL_NAME;
@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannels() {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("this is the description of the channel.");
notificationChannel.setLightColor(Color.RED);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
}
public void sendHighPriorityNotification(String title, String body, Class activityname) {
Intent intent = new Intent(this, activityname);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 267, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
// .setContentTitle(title)
// .setContentText(body)
.setSmallIcon(R.drawable.ic_launcher_background)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setStyle(new NotificationCompat.BigTextStyle().setSummaryText("Überwachungskamera").setBigContentTitle(title).bigText(body))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
NotificationManagerCompat.from(this).notify(new Random().nextInt(), notification);
}
}
它与我之前在旧应用程序中使用的代码完全相同,并且一切正常。所以我真的不想对代码做太多改动,因为它应该可以完美运行。也许我在别的地方忘了什么?也许问题不是代码...但我没有找到解决方案,也许你有。
编辑:这是“运行”下的红色文本
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mapbox1, PID: 32082
java.lang.RuntimeException: Unable to start receiver com.example.mapbox1.GeofenceBroadcastReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3399)
at android.app.ActivityThread.-wrap18(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1780)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:655)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
at com.example.mapbox1.HauptActivity.changeMeldungGefahr(HauptActivity.java:221)
at com.example.mapbox1.GeofenceBroadcastReceiver.onReceive(GeofenceBroadcastReceiver.java:47)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3392)
at android.app.ActivityThread.-wrap18(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1780)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
并说明一下:应用程序启动时,我会看到“HauptActivity”,然后我会根据需要收到通知。但是收到通知后应用程序立即崩溃。
来自日志:调用
HauptActivity.changeMeldungGefahr(HauptActivity.java:221)
导致空指针。
您应该修复 changeMeldungGefahr()
内的空指针。