通过监控演示应用程序获取该区域

时间:2014-03-15 13:59:15

标签: android region ibeacon-android

我尝试获取由我的应用输入的区域,但我只得到错误

03-15 02:41:37.453: E/AndroidRuntime(3420): FATAL EXCEPTION:   
IntentService[IBeaconIntentProcessor]
03-15 02:41:37.453: E/AndroidRuntime(3420): Process: com.example.beaconwithandroid, PID:   
3420
03-15 02:41:37.453: E/AndroidRuntime(3420): java.lang.NullPointerException 
03-15 02:41:37.453: E/AndroidRuntime(3420):     at
com.example.beaconwithandroid.Monitoring$1.didEnterRegion(Monitoring.java:54)
03-15 02:41:37.453: E/AndroidRuntime(3420):     at 
com.radiusnetworks.ibeacon.IBeaconIntentProcessor.onHandleIntent(IBeaconIntentProcessor.java:89)
03-15 02:41:37.453: E/AndroidRuntime(3420):     at 
android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) 
03-15 02:41:37.453: E/AndroidRuntime(3420):     at   
android.os.Handler.dispatchMessage(Handler.java:102)
03-15 02:41:37.453: E/AndroidRuntime(3420):     at   
android.os.Looper.loop(Looper.java:136)
03-15 02:41:37.453: E/AndroidRuntime(3420):     at android.os.HandlerThread.run(HandlerThread.java:61)

我使用3个信标,并从该网站获取代码

http://developer.radiusnetworks.com/ibeacon/android/samples.html

代码有效,直到我不想探索方法中的区域,如下所示:

Log.i(TAG, "I just saw an iBeacon for the firt time!" + region.getMajor().toString());   

,这看起来像我的活动,

public class Monitoring extends Activity implements IBeaconConsumer {


protected static final String TAG = "RangingActivity";
// instance of beacon manager which menage the beacon action
private IBeaconManager iBeaconManager = IBeaconManager.getInstanceForApplication(this) ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // bind with implemented IBeaconConsumer
    iBeaconManager.bind(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onIBeaconServiceConnect() {
    iBeaconManager.setMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) 
        {

          Log.i(TAG, "I just saw an iBeacon for the firt time!" + region.getMajor().toString());     

        }

        @Override
        public void didExitRegion(Region region) {
          Log.i(TAG, "I no longer see an iBeacon");
        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            Log.i(TAG, "I have just switched from seeing/not seeing iBeacons: "+state);     
        }
        });

        try {
            iBeaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
        } catch (RemoteException e) {   }

}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    iBeaconManager.unBind(this);
}
}

如果应用程序位于特定信标区域,我想用它来显示消息。

1 个答案:

答案 0 :(得分:0)

您得到NullPointerException,因为您的Major为null,稍后您在该null值上调用toString()Region构造函数中的第二个参数是Major值:

iBeaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));

此行导致异常,因为Major为null:

region.getMajor().toString()