使用 startMonitoringBeaconsInRegion 导致致命崩溃

Getting fatal crash using startMonitoringBeaconsInRegion

使用 android 信标库 2.16.3 我收到一个错误,我的应用程序在启动时崩溃。

此处似乎使用了此库并崩溃的部分。这两个部分都可以在 MainMenu class.

中找到

第 1 部分

beaconManager = BeaconManager.getInstanceForApplication(this);

beaconManager.getBeaconParsers().add(new BeaconParser().
        setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));

beaconManager.bind(this);

还有

第 2 部分

@Override
public void onBeaconServiceConnect() {
    final Region region = new Region("DanielBeaconNew",null, null, null);

    beaconManager.addMonitorNotifier(new MonitorNotifier()  {

        @Override
        public void didEnterRegion(Region region) {
            try {
                beaconManager.startRangingBeaconsInRegion(region);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void didExitRegion(Region region) {
            try {
                beaconManager.stopRangingBeaconsInRegion(region);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            System.out.println( "I have just switched from seeing/not seeing beacons: "+state);
        }
    });

    beaconManager.addRangeNotifier(new RangeNotifier() {

        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            for (Beacon b: beacons ) {
                mTextMessage.append(b.getBluetoothAddress() + " \n");
            }
        }
    });

    try {
        beaconManager.startMonitoringBeaconsInRegion(region);
    } catch (RemoteException e) {    }
}

初始绑定和最终 try catch 块是明显错误的来源。

堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: sc17dpc.individualproject, PID: 16111
    java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/localbroadcastmanager/content/LocalBroadcastManager;
        at org.altbeacon.beacon.BeaconLocalBroadcastProcessor.unregister(BeaconLocalBroadcastProcessor.java:80)
        at org.altbeacon.beacon.BeaconLocalBroadcastProcessor.register(BeaconLocalBroadcastProcessor.java:72)
        at org.altbeacon.beacon.service.ScanJobScheduler.ensureNotificationProcessorSetup(ScanJobScheduler.java:70)
        at org.altbeacon.beacon.service.ScanJobScheduler.schedule(ScanJobScheduler.java:130)
        at org.altbeacon.beacon.service.ScanJobScheduler.applySettingsToScheduledJob(ScanJobScheduler.java:85)
        at org.altbeacon.beacon.service.ScanJobScheduler.applySettingsToScheduledJob(ScanJobScheduler.java:92)
        at org.altbeacon.beacon.BeaconManager.applyChangesToServices(BeaconManager.java:1003)
        at org.altbeacon.beacon.BeaconManager.startMonitoringBeaconsInRegion(BeaconManager.java:938)
        at sc17dpc.individualproject.MainMenu.onBeaconServiceConnect(MainMenu.java:202)
        at org.altbeacon.beacon.BeaconManager.bind(BeaconManager.java:420)
        at sc17dpc.individualproject.MainMenu.onCreate(MainMenu.java:76)
        at android.app.Activity.performCreate(Activity.java:7458)
        at android.app.Activity.performCreate(Activity.java:7448)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3409)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.localbroadcastmanager.content.LocalBroadcastManager" on path: DexPathList[[zip file "/data/app/sc17dpc.individualproject-n6zKYGRXGKs9Lt6RAM16sw==/base.apk"],nativeLibraryDirectories=[/data/app/sc17dpc.individualproject-n6zKYGRXGKs9Lt6RAM16sw==/lib/arm64, /system/lib64, /product/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at org.altbeacon.beacon.BeaconLocalBroadcastProcessor.unregister(BeaconLocalBroadcastProcessor.java:80) 
        at org.altbeacon.beacon.BeaconLocalBroadcastProcessor.register(BeaconLocalBroadcastProcessor.java:72) 
        at org.altbeacon.beacon.service.ScanJobScheduler.ensureNotificationProcessorSetup(ScanJobScheduler.java:70) 
        at org.altbeacon.beacon.service.ScanJobScheduler.schedule(ScanJobScheduler.java:130) 
        at org.altbeacon.beacon.service.ScanJobScheduler.applySettingsToScheduledJob(ScanJobScheduler.java:85) 
        at org.altbeacon.beacon.service.ScanJobScheduler.applySettingsToScheduledJob(ScanJobScheduler.java:92) 
        at org.altbeacon.beacon.BeaconManager.applyChangesToServices(BeaconManager.java:1003) 
        at org.altbeacon.beacon.BeaconManager.startMonitoringBeaconsInRegion(BeaconManager.java:938) 
        at sc17dpc.individualproject.MainMenu.onBeaconServiceConnect(MainMenu.java:202) 
        at org.altbeacon.beacon.BeaconManager.bind(BeaconManager.java:420) 
        at sc17dpc.individualproject.MainMenu.onCreate(MainMenu.java:76) 
        at android.app.Activity.performCreate(Activity.java:7458) 
        at android.app.Activity.performCreate(Activity.java:7448) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3409) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199) 
        at android.os.Handler.dispatchMessage(Handler.java:112) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7625) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 

代码的问题似乎不在于代码的任何其他部分,因为当我注释掉最后一个 try catch 块时应用程序似乎运行良好。

我插入了第 1 部分和第 2 部分,因为它们是应用程序错误日志中唯一引用的部分。

第 1 部分包含第 76 行,第 2 部分包含第 202 行(startMonitoringBeaconsInRegion 调用)。

因为你的错误是:

 Caused by: java.lang.ClassNotFoundException: 
 Didn't find class "androidx.localbroadcastmanager.content.LocalBroadcastManager"

您可能需要添加依赖项,以便可以将 AndroidX 中的 LocalBroadCastManager 用于您的 build.gradle 依赖项:

 implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'