I was writing some code to connect my app to an iBeacon following the examples for Android, but my code continue to crash when i copy and paste these lines:
adapter = new LeDeviceListAdapter(this);
ListView list = (ListView) findViewById(R.id.device_list);
list.setAdapter(adapter);
list.setOnItemClickListener(createOnItemClickListener());
expecially on "list.setAdapter(adapter);" and generates NullPointerException in the "adapter".
Originally I was using the class LeDeviceListAdapter without the Inflater, but I changed my mind and I went back to the original implementation.
and the activity is the same in question's text:
public class BluetoothActivity extends Activity { some global var
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
// Configure verbose debug logging.L.enableDebugLogging(true);
// Configure device list.
adapter = newLeDeviceListAdapter(this);
ListView list = (ListView) findViewById(R.id.device_list);
list.setAdapter(adapter);
list.setOnItemClickListener(createOnItemClickListener());
// Configure BeaconManager.
beaconManager = newBeaconManager(this);
beaconManager.setRangingListener(newBeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, final List<Beacon> beacons) {
// Note that results are not delivered on UI thread.
runOnUiThread(newRunnable() {
@Override
public void run() {
// Note that beacons reported here are already sorted by estimated// distance between device and beacon.
adapter.replaceWith(beacons);
}
});
}
});
Yes, and also the Android Demo.
Maybe I'm missing some code?
In onCreate i have
// Configure BeaconManager.
beaconManager = new BeaconManager(this);
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, final List<Beacon> beacons) {
// Note that results are not delivered on UI thread.
runOnUiThread(new Runnable() {
@Override
public void run() {
// Note that beacons reported here are already sorted by estimated
// distance between device and beacon.
adapter.replaceWith(beacons);
}
});
}
});
and in my method I have
private void connectToService() {
TextView iBeacon = (TextView) findViewById(R.id.beaconConnectionText);
iBeacon.setText(CONNECTINGTOBEACON);
adapter.replaceWith(Collections.<Beacon>emptyList());
beaconManager.connect(new BeaconManager.ServiceReadyCallback(){
@Override
public void onServiceReady() {
try {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
} catch (RemoteException e) {
Toast.makeText(BluetoothActivity.this, "Cannot start ranging, something terrible happened",
Toast.LENGTH_LONG).show();
Log.e(TAG, "Cannot start ranging", e);
}
}
});
}
private AdapterView.OnItemClickListener createOnItemClickListener() {
return new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, longid) {
if (getIntent().getStringExtra(EXTRAS_TARGET_ACTIVITY) != null) {
try {
Class<?> clazz = Class.forName(getIntent().getStringExtra(EXTRAS_TARGET_ACTIVITY));
Intent intent = new Intent(BluetoothActivity.this, clazz);
intent.putExtra(EXTRAS_BEACON, adapter.getItem(position));
startActivity(intent);
} catch (ClassNotFoundException e) {
Log.e(TAG, "Finding class by name failed", e);
}
}
}
};
}