从在UI线程中运行代码的角度来看,之间有什么区别:
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Log.d("UI thread", "I am the UI thread");
}
});
Run Code Online (Sandbox Code Playgroud)
要么
MainActivity.this.myView.post(new Runnable() {
public void run() {
Log.d("UI thread", "I am the UI thread");
}
});
Run Code Online (Sandbox Code Playgroud)
和
private class BackgroundTask extends AsyncTask<String, Void, Bitmap> {
protected void onPostExecute(Bitmap result) {
Log.d("UI thread", "I am the UI thread");
}
}
Run Code Online (Sandbox Code Playgroud) 我需要检查运行某段代码的线程是否是主(UI)线程.我怎样才能做到这一点?
我在无法复制的产品中崩溃了。跟踪没有说明崩溃的位置。进行一些搜索后,看起来它可能与notifyDataSetChanged()有关。它发生在Android 6和7上。
我有2个使用listview的软件包:
BroadcastReceiver检查wifi AP的列表:在这种情况下,代码将收集所有AP,将它们添加到List中并调用notifyDataSetChanged:
@Override
public void onReceive(Context context, Intent intent) {
if (wifiManager != null) {
if (wifiManager.isWifiEnabled()) {
List<ScanResult> listeScan = wifiManager.getScanResults();
listeWifiItem.clear();
for (ScanResult scanResult : listeScan) {
WifiItem item = new WifiItem();
item.setAdresseMac(scanResult.BSSID);
item.setAPName(scanResult.SSID);
item.setForceSignal(scanResult.level);
listeWifiItem.add(item);
}
wifiAdapter.notifyDataSetChanged();
} else {
Toast.makeText(context, "You must activate the WiFi", Toast.LENGTH_SHORT);
}
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个异步任务,其中我在onPostExecute方法中调用notifyDataSetChanged:
@Override
protected void onPostExecute(Void result) {
iPadaptater.notifyDataSetChanged();
super.onPostExecute(result);
}
Run Code Online (Sandbox Code Playgroud)
我使用notifyDataSetChanged的方式是否有问题?在异步任务中,如果用户转到其他功能,我认为这样做更安全
您是否认为崩溃与notifyDataSetChanged有关?
您是否从痕迹中看到另一个重要信息?
这是两种跟踪类型:
类型1:
java.lang.IllegalStateException:
at android.widget.ListView.layoutChildren (ListView.java:1747)
at android.widget.AbsListView$CheckForTap.run (AbsListView.java:4728)
at android.os.Handler.handleCallback …Run Code Online (Sandbox Code Playgroud)