当推送通知即将到来时,Android 应用程序崩溃

如何解决当推送通知即将到来时,Android 应用程序崩溃

我有一个用于地理围栏的广播接收器。完全相同的代码在另一个应用程序中工作,我在一个活动中有一个完整的谷歌地图。在新应用中,我希望 Google 地图(片段)与其他按钮和文本一起作为一个活动的一部分。

当我将它添加到 Manifest.xml(见下文)时,应用程序打开,当我收到通知(我真的收到我想要的通知)时,应用程序崩溃。

<receiver
            android:name=".GeofencebroadcastReceiver"
            android:enabled="true"
            android:exported="true"></receiver>

没有这个 应用程序可以工作,但我没有收到通知

所以在某些时候这部分很重要,但它可能会阻止其他东西?

这是 GeofencebroadcastReceiver.java:(最后是我调用通知的开关/案例)

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() 内修复 Nullpointer。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?