gpt4 book ai didi

java - setText 和 findViewById 不在 OnCreate 中导致应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 09:52:18 28 4
gpt4 key购买 nike

我正在尝试更改文本标签以输出当前范围值,因此我在 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,],,

最佳答案

onBeaconServiceConnect() 回调仅用于响应 beaconManager.bind(this); 调用,该调用位于 onCreate()。根据显示的代码,我认为不可能在 onCreate 方法之前调用 onBeaconServiceConnect() ,因此 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());
}
});

关于java - setText 和 findViewById 不在 OnCreate 中导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37530013/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com