AltBeacon application crashes on Android tab

非 Y 不嫁゛ 提交于 2019-12-12 04:56:55

问题


I have been trying to use the AltBeacon Monitoring Example Code to monitor my Kontakt.io beacon as described http://altbeacon.github.io/android-beacon-library/samples.html here. I could import the library (android-beacon-library-2.1.3.aar) in my android studio (1.0.2) and there is no error in the monitoring code. But when I want to run it on my tab (LG Tab 7 API 19) it stops working with a message on the screen. I can be sure that the Altbeacon lib is imported successfully as it appears in the list when i use import. in my MainActivity.java. I didnt change the code in any way. Logcat says the following:

11988-11988/xxx.altbeeklibtest1 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41747e48)
11988-11988/xxxx.altbeeklibtest1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: xxxx.altbeeklibtest1, PID: 11988
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{xxxx.altbeeklibtest1/xxxx.altbeeklibtest1.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:94)
            at org.altbeacon.beacon.BeaconManager.verifyServiceDeclaration(BeaconManager.java:707)
            at org.altbeacon.beacon.BeaconManager.<init>(BeaconManager.java:233)
            at org.altbeacon.beacon.BeaconManager.getInstanceForApplication(BeaconManager.java:211)
            at com.example.shaffat.altbeeklibtest1.MainActivity.<init>(MainActivity.java:18)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1208)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2119)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
            at dalvik.system.NativeStart.main(Native Method)

What am I doing wrong? Help is highly appriciated


回答1:


The example inits beaconManager this way:

private BeaconManager beaconManager = beaconManager.getInstanceForApplication(this);

You should do it inside onCreate() method of your Activity.

public class MainActivity extends Activity  implements BeaconConsumer{
    protected static final String TAG = "RangingActivity";
    private BeaconManager;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
       beaconManager = BeaconManager.getInstanceForApplication(this);
       beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0216,i:4-19,i:20-21,i:22-23,p:24-24"));
         // set the duration of the scan to be 1.1 seconds
        beaconManager.setBackgroundScanPeriod(1100l); 
        // set the time between each scan to be 1 hour (3600 seconds)
        beaconManager.setBackgroundBetweenScanPeriod(1000);

        beaconManager.bind(this);

    }



回答2:


I would need to see the code that calls BeaconManager.getInstanceForApplication(context) to be sure, but the stack trace suggests this code is receiving a null context. The passed context must be a non-null, valid instance of an Activity, Application or Service.



来源:https://stackoverflow.com/questions/28962436/altbeacon-application-crashes-on-android-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!