Question

I try to get region which is entered by my app, bu I get only errors

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)

I use 3 beacons, and took the code from this site

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

the code works until I don't want to explore the region which is in the method like in this line:

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

, this how looks my activity,

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

I wanted to use it to show message if the app is in the region of particular beacon.

Était-ce utile?

La solution

You get the NullPointerException because your Major is null and you later call toString() on that null value. The second parameter in the Region constructor is the Major value:

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

This line is causing the exception because Major is null:

region.getMajor().toString()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top