- 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/
我有一个问题,但由于 this question 部分正在解决,但我想知道如何计算给定间隔之间的天数。 这是一个计算员工休假天数的查询。所以给定(或不给定)一个日期范围,我想计算给定间隔之间有多少假期
变量dateSubtract结果是 16,但我想找到这 2 天之间的总天数,应该是 165。没有 JODA TIME 我该如何做到这一点? String date = "06/17/2014"; Da
我想选择创建日期介于给定月份的第一天和最后一天之间的记录。我通过以下方式计算开始日期和结束日期的月份: 日期“月份”只是时间范围内的随机日期 Calendar cal = Calendar.getIn
我有一个对你们大多数人来说可能微不足道的问题。我尝试了很多,没有找到解决方案,所以如果有人能给我提示,我会很高兴。起点是每周 xts -时间序列。 月周值(value)目标 2011 年 12 月 W
我有一个 Facebook 应用程序,它将用户生日作为 varchar 存储在 mysql 数据库中。我正在尝试获取所有用户的生日 1周后推出,如果是在本周如果生日是上周。 在我的 php 中,我获取
我正在使用以下代码来获取年、月、日中的两个日期之间的差异 tenAppDTO.getTAP_PROPOSED_START_DATE()=2009-11-01 tenAppDTO.getTAP_PRO
我想检查当前时间(在 C++ 中)是否在一个时间范围内。 我想从元组 ("12:00", "17:30") 构造时间范围,即 (string, string) 并检查时间 now() 是否介于两者之间
gitlab 有一个功能,如果我在提交消息中放入票号,那么提交将与 gitlab.com 上的票相关联。 这在进行代码审查时非常方便。不幸的是,开发人员有时会忘记这样做。 我想指定 git hooks
我正在尝试制作使用SQLite数据库的简单注册/登录应用程序,到目前为止我得到了这段代码。这是我的“注册” Activity ,我猜它应该在按下注册按钮后将用户名和 pin(密码)实现到数据库,遗憾的
我正在尝试打开、关闭和写入文件。每当我尝试打开一个文件时,如果我提供的路径中不存在该文件,程序就会告诉我。如果存在,程序将读取其中的内容并显示它。如果用户不想查找文件,可以选择创建文件并用数据填充它。
我想要我的至slideToggle每当发生 react 性变化时,但到目前为止我还无法使其发生。我尝试在 rendered 中使用 JQuery和created模板的事件,但它没有触发。 触发此操作的
我们的 MySQL 遇到了神秘的网络问题。简单的更新查询(使用索引更新单行)通常会立即运行,然后有时(假设 1000 次中有 1 次)因超时而失败。与简单的插入查询相同。数据库没有过载。我们怀疑网络问
我正在使用 actionbarsherlock 的 ActionBar,第一次以横向或水平方向运行应用程序时,选项卡以 Tabs Mode 显示。将方向更改为纵向后,导航模式仍在 Tabs 中。第二次
每天晚上(太平洋标准时间晚上 8 点)我都会对生产数据库(innoDB 引擎)进行全局备份。 这是 mysqldump 命令: mysqldump -u$MYSQLUSER -p$MYSQLPWD -
当我的应用程序第一次启动时,它应该显示用户协议(protocol),这是一个 59kb 的 txt 文件。由于读取文件并将其附加到 TextView 需要一些时间,因此我决定在异步任务中执行此操作并在
如何只允许一个“.”在按键期间的javascript中? 我这里有一个代码: function allowOneDot(txt) { if ((txt.value.split(".")
我已经创建了像主页和用户这样的标题图标。在桌面 View 中,如果我单击用户图像,它会显示相应的重定向页面。如果我在选项卡或移动 View 中将其最小化, 它什么都不显示。此问题仅发生在用户图像上,而
下面的代码在 Release模式下工作,并且仅在 Debug模式下在 g_ItemList.push_back() 引发错误,我浏览了一些 SO 帖子和论坛。有人提到 "You can't itera
我遇到了一个我似乎无法解决的 mmap 问题。下面是设置:我使用 malloc 将一个巨大的多维数组分配到内存中,用我的值填充它,然后我想将它保存在一个文件中。该数组包含 3200000000 个字节
尝试加载共享库: handle = dlopen( "libaaa.so.2.5", RTLD_NOW ); if ( !handle ) { printf("Failed t
我是一名优秀的程序员,十分优秀!