我一直在尝试使用AltBeacon监控示例代码来监控我的Kontakt.io信标,如http://altbeacon.github.io/android-beacon-library/samples.html所述。我可以在我的android studio(1.0.2)中导入库(android-beacon-library-2.1.3.aar),监控代码中没有错误。但是,当我想在我的选项卡(LG Tab 7 API 19)上运行它时,它将停止处理屏幕上的消息。
当我在MainActivity.java中使用import.
时,我可以确定Altbeacon lib已成功导入,因为它出现在列表中。我没有以任何方式更改代码。
Logcat说如下:
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)
我做错了什么?帮助很高兴
答案 0 :(得分:3)
这个例子以beaconManager的方式出现:
private BeaconManager beaconManager = beaconManager.getInstanceForApplication(this);
您应该在Activity的onCreate()方法中执行此操作。
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);
}
答案 1 :(得分:0)
我需要查看调用BeaconManager.getInstanceForApplication(context)
的代码以确定,但堆栈跟踪表明此代码正在接收空上下文。传递的上下文必须是活动,应用程序或服务的非null,有效实例。