- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个应用程序来扫描蓝牙设备。如果我在扫描过程中滚动,应用程序就会崩溃。否则,如果我将其保留为 scan_period,它就可以正常工作。
这是我的主要 Activity :
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeScanner mBluetoothLeScanner;
private boolean mScanning;
private static final int ENABLE_BLUETOOTH = 1;
private static final int ENABLE_LOCATION = 255;
Button btnScan;
ListView listViewLE;
List<BluetoothDevice> listBluetoothDevice;
ListAdapter adapterLeScanResult;
private Handler mHandler;
private static final long SCAN_PERIOD = 7500;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//check BLE supported on this device or not
if(!getPackageManager().hasSystemFeature(getPackageManager().FEATURE_BLUETOOTH_LE)){
Toast.makeText(this,"Bluetooth Low Energy is not supported.", Toast.LENGTH_SHORT).show();
finish();
}
//get bluetooth adapter and le scanner
btAdapterAndleScanner();
//check if bluetooth is supported on this device
if(mBluetoothAdapter == null){
Toast.makeText(this, "Bluetooth is not supported.", Toast.LENGTH_SHORT).show();
finish();
return;
}
//check for location permission
locationPermission();
btnScan = (Button)findViewById(R.id.scan_button);
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanLeDevice(true);
btnScan.setVisibility(View.GONE);
}
});
listViewLE = (ListView)findViewById(R.id.device_list);
listBluetoothDevice = new ArrayList<BluetoothDevice>();
adapterLeScanResult = new ArrayAdapter<BluetoothDevice>(this,android.R.layout.simple_expandable_list_item_1, listBluetoothDevice);
listViewLE.setAdapter(adapterLeScanResult);
listViewLE.setOnItemClickListener(scanResultOnItemClickListener);
mHandler = new Handler();
}
AdapterView.OnItemClickListener scanResultOnItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final BluetoothDevice device = (BluetoothDevice) parent.getItemAtPosition(position);
//TODO: Add bondstate to check whether the device is bonded or not.
String msg = "Address: " + device.getAddress() + "\n" + "Type: " + getBTDeviceType(device);
new AlertDialog.Builder(MainActivity.this)
.setTitle("Name: " + device.getName())
.setMessage(msg)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
};
private String getBTDeviceType(BluetoothDevice d){
String type = "";
switch (d.getType()){
case BluetoothDevice.DEVICE_TYPE_CLASSIC:
type = "Classic";
break;
case BluetoothDevice.DEVICE_TYPE_DUAL:
type = "Dual";
break;
case BluetoothDevice.DEVICE_TYPE_LE:
type = "Low Energy";
break;
case BluetoothDevice.DEVICE_TYPE_UNKNOWN:
type = "Unknown";
break;
default:
type = "unknown...";
}
return type;
}
@Override
protected void onResume(){
super.onResume();
//TODO: use if condition twice to check how it differes from using it from the first time.
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, ENABLE_BLUETOOTH);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == ENABLE_BLUETOOTH && resultCode == Activity.RESULT_CANCELED){
finish();
return;
}
//TODO: get bluetooth adapter and lescan function to see what happens. go without it first.
//TODO: check bluetooth supported or not. no idea why!!!
super.onActivityResult(requestCode, resultCode, data);
}
private void btAdapterAndleScanner(){
final BluetoothManager mBluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
mScanning = false;
}
private void scanLeDevice(final boolean enable){
if (enable){
listBluetoothDevice.clear();
listViewLE.invalidateViews();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mBluetoothLeScanner.stopScan(scanCallback);
listViewLE.invalidateViews();
Toast.makeText(MainActivity.this, "Scan timeout", Toast.LENGTH_SHORT).show();
mScanning = false;
btnScan.setEnabled(true);
}
},SCAN_PERIOD);
mBluetoothLeScanner.startScan(scanCallback);
mScanning = true;
btnScan.setEnabled(false);
} else {
mBluetoothLeScanner.stopScan(scanCallback);
mScanning = false;
btnScan.setEnabled(true);
}
}
private ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
addBluetoothDevice(result.getDevice());
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
for (ScanResult result : results){
addBluetoothDevice(result.getDevice());
}
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
Toast.makeText(MainActivity.this, "onScanFailed: " + String.valueOf(errorCode),Toast.LENGTH_LONG).show();
}
private void addBluetoothDevice(BluetoothDevice device){
if(!listBluetoothDevice.contains(device)){
listBluetoothDevice.add(device);
listViewLE.invalidateViews();
}
}
};
@TargetApi(23)
private void locationPermission() {
if (ContextCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION) != PERMISSION_GRANTED) {
requestPermissions(new String[]{ACCESS_COARSE_LOCATION}, ENABLE_LOCATION);
}
}
}
这是我的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/device_list"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Scan"
android:id="@+id/scan_button"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
这是日志猫:
2019-09-30 15:38:29.036 2786-2786/com.example.v_daq_re E/InputEventReceiver: Exception dispatching input event.
2019-09-30 15:38:29.037 2786-2786/com.example.v_daq_re E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2019-09-30 15:38:29.043 2786-2786/com.example.v_daq_re E/MessageQueue-JNI: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131230812, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1810)
at android.widget.AbsListView.onTouchMove(AbsListView.java:6514)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:6368)
at android.widget.ListView.onTouchEvent(ListView.java:1705)
at android.view.View.dispatchTouchEvent(View.java:13484)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2888)
at android.widget.AbsListView.dispatchTouchEvent(AbsListView.java:6285)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:697)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1879)
at android.app.Activity.dispatchTouchEvent(Activity.java:3487)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:655)
at android.view.View.dispatchPointerEvent(View.java:13732)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6119)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5897)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5346)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5399)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5365)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5524)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5373)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5581)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5346)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5399)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5365)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5373)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5346)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8408)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8341)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8294)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8523)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:181)
at android.app.ActivityThread.main(ActivityThread.java:7037)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494
2019-09-30 15:38:29.043 2786-2786/com.example.v_daq_re D/AndroidRuntime: Shutting down VM
2019-09-30 15:38:29.045 2786-2786/com.example.v_daq_re E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.v_daq_re, PID: 2786
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131230812, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1810)
at android.widget.AbsListView.onTouchMove(AbsListView.java:6514)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:6368)
at android.widget.ListView.onTouchEvent(ListView.java:1705)
at android.view.View.dispatchTouchEvent(View.java:13484)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2888)
at android.widget.AbsListView.dispatchTouchEvent(AbsListView.java:6285)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:697)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1879)
at android.app.Activity.dispatchTouchEvent(Activity.java:3487)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:655)
at android.view.View.dispatchPointerEvent(View.java:13732)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6119)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5897)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5346)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5399)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5365)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5524)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5373)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5581)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5346)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5399)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5365)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5373)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5346)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8408)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8341)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8294)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8523)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:181)
at android.app.ActivityThread.main(ActivityThread.java:7037)
2019-09-30 15:38:29.045 2786-2786/com.example.v_daq_re E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
应用程序本身运行良好。唯一的问题是当我在扫描过程中向下滚动时。如果有人能找出问题并让我知道如何解决它,那将会很有帮助。谢谢!!!
最佳答案
The content of the adapter has changed but ListView did not receive a notification.
Make sure the content of your adapter is not modified from a background thread, but only from the UI thread.
Make sure your adapter calls notifyDataSetChanged() when its content changes.
编辑:正如 @Codo 评论的那样,在这种情况下,只有 notifyDataSetChanged()
没有被调用。
修改listBluetoothDevice
后在适配器上调用notifyDataSetChanged()
如果从另一个线程更改listBluetoothDevice
。用 runOnUiThread
包装它并调用 notifyDataSetChanged()
runOnUiThread(new Runnable() {
@Override
public void run() {
//...
}
});
关于如果在蓝牙扫描期间滚动,则出现 java.lang.IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58161940/
我有一个坦克射击弹药的游戏。我对这部分代码的目标是检查它们是否与“碰撞”磁贴发生碰撞,如果是,则将其和磁贴移除。 代码如下所示,每 1/60 秒检查一次: Iterator iterator = sh
我尝试使用 JSR-303 注释(类级别)和验证器实现为 play 2.0.1 编写自定义表单验证器。 不幸的是,当我提交表单并且验证失败时,我收到了一个 IllegalStateException,
根据answer of BalusC ,我用过 FacesContext.getCurrentInstance().getExternalContext().redirect(url); 在我的 @P
这个问题已经有答案了: Copy a stream to avoid "stream has already been operated upon or closed" (10 个回答) 已关闭 5
这个问题已经有答案了: Spring: getOutputStream() has already been called for this response (3 个回答) 已关闭 4 年前。 我正
我正在尝试将 Activity 转换为 FragmentActivty 对象,以便获得 FragmentManager 对象 public class Main extends ListActivit
我正在尝试使用可编辑的组合框,通过用户的某些击键从数据库中快速搜索客户端的功能。我想要的是,用户将输入一些字母,如果这些字母与某些客户端匹配,这些客户端将保留在组合框的当前数据模型中。 代码如下。请修
这个问题已经有答案了: You need to use a Theme.AppCompat theme (or descendant) with this activity. Change to Th
我正在使用 Android Studio 和 Genymotion 作为模拟器创建一个应用程序,其中我在 3 个 EditText 中输入数据,当我单击按钮将其存储在 sqlite 数据库中时,它不起
我正在为 Android 构建一个简单的消息应用程序,并且在发送短信时遇到一些问题。我第一次使用 OnlickListener 时,消息被发送并显示在我的 ListView 中。当我在 Activit
我了解到 collect() 和 forEach() 都是流终端操作,在同一个流上调用它们会抛出 非法状态异常。但是,以下代码可以成功编译并按升序打印每个字符串的长度。不会引发任何异常。怎么会这样?
我对 classcastException 和非法状态异常都有点困惑,因为在大多数情况下它们看起来都很相似。 我在这个java代码中遇到了一个问题 class consumer {
我正在尝试这个小计算器程序。当我调用calculateResult()方法时,我想在第二个操作数为零且运算符为除法时显示IllegalStateException错误。但尽管如此,我在calculat
Stacktrace Here 导入java.util.*; 公共(public)类 AccountClient { public static void main(String[] args) {
我正在使用 readEntity() 方法读取 JAVAX 响应,但我收到以下堆栈跟踪: java.lang.IllegalStateException: Entity input stream ha
我是安卓新手。我正在尝试进行简单的登录 Activity ,但当我单击“登录”按钮时出现运行时错误。我认为我没有正确获取数据。我已经检查过,SQLite 中有一个与该 PK 相对应的数据。 日志猫。
我正在创建一个登录页面,工程师可以通过以“engg”开头的用户名登录。问题出在登录页面,当我使用正确的密码提供正确的输入时,它会给出“非法状态异常”。在错误的输入中,它工作正常。就像当我在我的 ora
我正在使用一些现有的 Java 设备驱动程序软件,该软件使用 JavaCOMM 进行串行 I/O。我昨天看到它抛出一个异常,其中有一个 IllegalStateException - 端口从 publ
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我正在使用 Adventnet SNMPAPI 开发 UDP 监听程序。现在我需要将监听数据保存到数据库中。当我这样做时,我遇到了错误。任何人都可以帮忙解决这个问题吗... 这是我的代码。 impor
我是一名优秀的程序员,十分优秀!