我正在尝试更改文本标签以输出当前范围值,因此我在MainActivity的开头创建了一个变量 TextView rangeElement; 。
在我的OnCreate方法中,我将其链接到放置在我的XML文件中的ID,其中 rangeElement =(TextView)findViewById(R.id.range);
完美无缺。但我有另一种方法,它正在寻找蓝牙信标,并给我我想要打印的范围。它被称为 void onBeaconServiceConnect(),并在信标服务运行并准备通过BeaconManager接受命令时启动。这似乎发生在我的View加载之前,所以我的应用程序一旦onBeaconServiceConnect尝试使用 rangeElement.setText(“Range:”+ oneBeacon.getDistance()); <访问变量就崩溃了/ p>
我正在使用 Android Beacon Library 2.8 API - 有人知道如何在尝试更改文字标签时阻止应用崩溃吗?
PS:这是我的完整代码
public class BeaconActivity extends ActionBarActivity implements BeaconConsumer{
public static final String TAG = "BeaconsEverywhere";
// Beacon Manager Variable
private BeaconManager beaconManager;
TextView rangeElement;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_beacon);
// Variablen für Elemente, die in der XML angepasst werden sollen
rangeElement = (TextView) findViewById(R.id.range);
// Instantiieren des Beacon Managers mit Factory Method
beaconManager = BeaconManager.getInstanceForApplication(this);
// Sag der Library wie die Signale decodiert werden sollen
beaconManager.getBeaconParsers().add(new BeaconParser()
// 2015=beac
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager.bind(this);
}
// Implementieren der BeaconConsumer Methode
@Override
public void onBeaconServiceConnect() {
final org.altbeacon.beacon.Region region = new org.altbeacon.beacon.Region("myBeacons", Identifier.parse("73676723-7400-0000-ffff-0000ffff0005"), null, null);
beaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(org.altbeacon.beacon.Region region) {
try {
Log.d(TAG, "did Enter Region");
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didExitRegion(org.altbeacon.beacon.Region region) {
try {
Log.d(TAG, "did Exit Region");
beaconManager.stopRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didDetermineStateForRegion(int i, org.altbeacon.beacon.Region region) {
}
});
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
//Log out welche beacons in der Nähe sind
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, org.altbeacon.beacon.Region region) {
for(Beacon oneBeacon : beacons) {
Log.d(TAG, "distance: " + oneBeacon.getDistance() + "id: " + oneBeacon.getId1() + "/" + oneBeacon.getId2() + "/" + oneBeacon.getId3());
//rangeElement.setText("Range: " + oneBeacon.getDistance());
}
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
PPS:我在RuntimeInit $ UncaughtHandler(com.android.internal.os)的第72行中发生未被捕获的异常时导出该线程:
main@20986, prio=5, in group 'main', status: 'RUNNING'
at android.os.BinderProxy.transactNative(Binder.java:-1)
at android.os.BinderProxy.transact(Binder.java:496)
at android.bluetooth.IBluetoothGatt$Stub$Proxy.registerClient(IBluetoothGatt.java:889)
at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper.startRegisteration(BluetoothLeScanner.java:286)
at android.bluetooth.le.BluetoothLeScanner.startScan(BluetoothLeScanner.java:140)
at android.bluetooth.le.BluetoothLeScanner.startScan(BluetoothLeScanner.java:104)
at org.altbeacon.beacon.service.scanner.CycledLeScannerForLollipop.startScan(CycledLeScannerForLollipop.java:187)
at org.altbeacon.beacon.service.scanner.CycledLeScanner.scanLeDevice(CycledLeScanner.java:190)
at org.altbeacon.beacon.service.scanner.CycledLeScanner.finishScanCycle(CycledLeScanner.java:266)
at org.altbeacon.beacon.service.scanner.CycledLeScanner.scheduleScanCycleStop(CycledLeScanner.java:239)
at org.altbeacon.beacon.service.scanner.CycledLeScanner$1.run(CycledLeScanner.java:235)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6873)
at java.lang.reflect.Method.invoke(Method.java:-1)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Signal Catcher@21301 daemon, prio=5, in group 'system', status: 'WAIT'
Incompatible thread state: thread not suspended
ReferenceQueueDaemon@21302 daemon, prio=5, in group 'system', status: 'WAIT'
blocks ReferenceQueueDaemon@21302
at java.lang.Object.wait(Object.java:-1)
at java.lang.Daemons$ReferenceQueueDaemon.run(Daemons.java:149)
at java.lang.Thread.run(Thread.java:818)
FinalizerDaemon@21303 daemon, prio=5, in group 'system', status: 'WAIT'
blocks FinalizerDaemon@21303
at java.lang.Object.wait(Object.java:-1)
at java.lang.Object.wait(Object.java:422)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:101)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:72)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:189)
at java.lang.Thread.run(Thread.java:818)
FinalizerWatchdogDaemon@21304 daemon, prio=5, in group 'system', status: 'WAIT'
blocks FinalizerWatchdogDaemon@21304
at java.lang.Object.wait(Object.java:-1)
at java.lang.Daemons$FinalizerWatchdogDaemon.waitForObject(Daemons.java:255)
at java.lang.Daemons$FinalizerWatchdogDaemon.run(Daemons.java:227)
at java.lang.Thread.run(Thread.java:818)
Binder_1@21305, prio=5, in group 'main', status: 'RUNNING'
Incompatible thread state: thread not suspended
HeapTrimmerDaemon@21306 daemon, prio=5, in group 'system', status: 'WAIT'
blocks HeapTrimmerDaemon@21306
at java.lang.Object.wait(Object.java:-1)
at java.lang.Daemons$HeapTrimmerDaemon.run(Daemons.java:326)
at java.lang.Thread.run(Thread.java:818)
GCDaemon@21307 daemon, prio=5, in group 'system', status: 'WAIT'
blocks GCDaemon@21307
at java.lang.Object.wait(Object.java:-1)
at java.lang.Daemons$GCDaemon.run(Daemons.java:359)
at java.lang.Thread.run(Thread.java:818)
Binder_2@21308, prio=5, in group 'main', status: 'RUNNING'
Incompatible thread state: thread not suspended
RenderThread@21189, prio=5, in group 'main', status: 'RUNNING'
Incompatible thread state: thread not suspended
hwuiTask1@21265, prio=5, in group 'main', status: 'RUNNING'
Incompatible thread state: thread not suspended
pool-2-thread-1@21274, prio=5, in group 'main', status: 'WAIT'
blocks pool-2-thread-1@21274
at java.lang.Object.wait(Object.java:-1)
at java.lang.Thread.parkFor(Thread.java:1220)
at sun.misc.Unsafe.park(Unsafe.java:299)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:157)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2016)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:410)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1035)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1097)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
pool-2-thread-2@21279, prio=5, in group 'main', status: 'WAIT'
blocks pool-2-thread-2@21279
at java.lang.Object.wait(Object.java:-1)
at java.lang.Thread.parkFor(Thread.java:1220)
at sun.misc.Unsafe.park(Unsafe.java:299)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:157)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2016)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:410)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1035)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1097)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
IntentService[BeaconIntentProcessor]@21291, prio=5, in group 'main', status: 'WAIT'
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:72)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
pool-2-thread-3@21296, prio=5, in group 'main', status: 'RUNNING'
at java.lang.IntegralToString.longToHexString(IntegralToString.java:508)
at java.lang.Long.toHexString(Long.java:443)
at java.util.UUID.toString(UUID.java:420)
at org.altbeacon.beacon.Identifier.toString(Identifier.java:242)
at org.altbeacon.beacon.Beacon.toStringBuilder(Beacon.java:502)
at org.altbeacon.beacon.Beacon.hashCode(Beacon.java:450)
at java.util.Collections.secondaryHash(Collections.java:3405)
at java.util.HashMap.get(HashMap.java:300)
at org.altbeacon.beacon.service.RangeState.addBeacon(RangeState.java:50)
at org.altbeacon.beacon.service.BeaconService.processBeaconFromScan(BeaconService.java:422)
at org.altbeacon.beacon.service.BeaconService.access$500(BeaconService.java:76)
at org.altbeacon.beacon.service.BeaconService$ScanProcessor.doInBackground(BeaconService.java:467)
at org.altbeacon.beacon.service.BeaconService$ScanProcessor.doInBackground(BeaconService.java:442)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
pool-2-thread-4@21314, prio=5, in group 'main', status: 'MONITOR'
waiting for pool-2-thread-3@21296 to release lock on <0x5358> (a java.util.HashMap)
at org.altbeacon.beacon.service.BeaconService.processBeaconFromScan(BeaconService.java:414)
at org.altbeacon.beacon.service.BeaconService.access$500(BeaconService.java:76)
at org.altbeacon.beacon.service.BeaconService$ScanProcessor.doInBackground(BeaconService.java:467)
at org.altbeacon.beacon.service.BeaconService$ScanProcessor.doInBackground(BeaconService.java:442)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Binder_3@21319, prio=5, in group 'main', status: 'RUNNING'
Incompatible thread state: thread not suspended
附加消息具有以下值: [F,A,T,A,L,E,X,C,E,P,T,I,O,N,:,, I,n,t,e,n,t,S, E,R,v,I,C,E,[,B,E,A,C,O,N,I,N,T,E,N,T,P,R,O,C,E,S, S,O,R,] ,
答案 0 :(得分:1)
onBeaconServiceConnect()
回调仅用于回复beaconManager.bind(this);
onCreate()
调用。根据显示的代码,我认为在onBeaconServiceConnect()
方法之前调用onCreate
是不可能的,因此必须执行rangeElement = (TextView) findViewById(R.id.range);
行。
一种解释是findViewById(R.id.range);
返回null。您确定此元素标识符是否在R.layout.activity_beacon中定义?如果你相信它,我会在onCreate
添加一个调试行,或者在那里设置一个断点来验证调用是否返回一个非空值。
编辑:另一种可能性是,尝试更新非UI线程上的UI会导致异常。信标回调通常不在UI线程上。尝试包装您的UI更新,如下所示:
runOnUiThread(new Runnable() {
public void run() {
rangeElement.setText("Range: " + oneBeacon.getDistance());
}
});
答案 1 :(得分:0)
执行此操作即可检查rangeElement是否已初始化,因此app不会崩溃:
")