【发布时间】:2022-01-05 00:01:32
【问题描述】:
我想检查编辑文本中的 URL,如果它有效,请在回收站视图中添加一个项目。所以为了这个目的,我启动了一个线程来检查 HTTP 连接。
thread = new Thread(new Runnable() {
@Override
public void run() {
String link = edt.getText().toString();
URL url = null;
try {
url = new URL(link);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int code = connection.getResponseCode();
if(code == 200) {
Log.d(TAG, "reachable");
InsertItem(url,adapter);
} else {
Log.d(TAG, "in catch: not reachable");
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
问题是我尝试添加项目时遇到的错误
private void InsertItem(URL url, MyAdapter adapter) {
thread.currentThread().interrupt();
arrayList.add(0,new file(url.toString()));
adapter.notifyItemChanged(0);
};
错误是:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
【问题讨论】:
标签: android multithreading android-recyclerview httpconnection