0

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.

MyWay
  • 1,011
  • 2
  • 14
  • 35
  • I'm not sure what you mean by the "code works until I don't want to explore the region". Does this mean you call stopMonitoringBeaconsInRegion before this happens? – davidgyoung Mar 15 '14 at 15:00
  • until I don't want to see a values of region by region.getMajor()/ getMinor() – MyWay Mar 16 '14 at 15:26

1 Answers1

0

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()
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • So every time I asked for region.getMajor() it take the value from declared region as u said. – MyWay Mar 16 '14 at 17:01