0

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

rubikskube
  • 372
  • 1
  • 5
  • 22

2 Answers2

3

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);

    }
Álvaro Pérez Soria
  • 1,443
  • 1
  • 13
  • 26
  • This is correct. Apologies, I wrote the example code for the 2.0 version of the library and didn't realize that this usage was broken as of the 2.1 version. I have updated the example code to construct the BeaconManager inside the onCreate method. – davidgyoung Mar 10 '15 at 20:20
0

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.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • The stack trace indicates it is being called from the class initializer, but an Activity class doesn't have a valid initialized Context until onCreate() is called, so the problem - as the other answer points out - is that the call is misplaced in the Activity code. – Chris Stratton Mar 10 '15 at 16:22
  • Good point, @ChrisStratton. The other answer is correct. I will update the example code to reflect this. – davidgyoung Mar 10 '15 at 20:15