【发布时间】:2017-02-20 21:29:35
【问题描述】:
我正在使用信标库开发一个 android 应用程序,该应用程序的一部分将通过其次要 ID(由用户通过对话框插入)搜索特定信标。
如果我在同一个活动中编写所有内容,则一切正常,但我想在外部纯 Java 类中保持对话框分开,因此在实现 BeaconConsumer 的活动中,我添加了一个“方法”,创建和绑定信标管理器。
public class Activity03 extends AppCompatActivity
implements BeaconConsumer, RangeNotifier {
...
public void scanForBeacon(Context context, String selectedMinorId){
beaconManager = BeaconManager.getInstanceForApplication(context);
beaconManager.getBeaconParsers().add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
Identifier minorIdFilter = Identifier.parse(selectedMinorId);
myRegion = new Region(
"my_region",
null,
null,
minorIdFilter);
beaconManager.bind((BeaconConsumer) context);
}
...
}
重点是当调用startRangingBeaconsInRegion时,我得到了:
尝试在空对象引用上调用虚拟方法“void org.altbeacon.beacon.BeaconManager.startRangingBeaconsInRegion(org.altbeacon.beacon.Region)”
顺序是:
1. 要求用户(通过 GoogleApiClient)开启 BLE 和本地化
2. 在 onActivityResult 中,插入次要 ID 的对话框是从 java 类 DialogUtilitiesDialogUtilities.showSelectionDialog(Activity03.this);
中挑选出来的
3. 按下按钮对话框被关闭,BeaconConsumer 活动的一个实例被创建并且方法被调用:Activity03 a03 = new Activity03();
a03.scanForBeacon(context, minorId);
4. 当 onBeaconServiceConnect() 被调用时,我在 startRangingBeaconsInRegion
@Override
public void onBeaconServiceConnect() {
try {
beaconManager.startRangingBeaconsInRegion(myRegion);
} catch (RemoteException e) {
e.printStackTrace();
}
}
我是 java 和 android 的新手,但在我看来,问题与区域无关,因为即使我将所有标识符都设置为 null,我也有相同的响应,所以我无法理解为什么为 null 引用。
由于 Dialog 返回的活动距离,我是否有可能创建两个不同的 BeaconMangers?如果是这样,我该如何解决?
如果不是,如何避免这个空对象引用?
在此先感谢
编辑
BeaconManager 声明
public class Activity03 extends AppCompatActivity implements BeaconConsumer, RangeNotifier {
static final int REQUEST_CHECK_SETTINGS = 1000;
private BeaconManager beaconManager;
private Region myRegion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppTheme);
setContentView(R.layout.activity_03);
}
@Override
protected void onResume() {
@Override
super.onResume();
PermissionsUtilities.switchLocationAndBluetooth(Activity03.this);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
DialogUtilities.showSensorSelectionDialog(Activity03.this);
break;
case Activity.RESULT_CANCELED:
...
break;
default:
break;
}
break;
}
}
【问题讨论】: