等待线程时如何编辑视图?
How to edit views when waiting for a thread?
我从我的 wordpress 网页加载我的文章。并想在我的应用程序中显示它们。
为了允许等待互联网响应,我必须创建一个线程。
使用此方法:
private void loadArticles(final String url) {
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
//Get my data and run createArticles();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
然后在 createArticles 函数中,我尝试使用我的函数 addViewToParent(...),但我收到一条错误消息:
Only the original thread that created a view hierarchy can touch its
views.
我该如何解决这个问题?
编辑:如果重要的话,我会使用这些网络功能...
//Connect to the url
URL pageURL = new URL(url);
HttpURLConnection connection = (HttpURLConnection) pageURL.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream())
);
只需改用处理程序,这样您就可以访问 UI 主题:
new Handler().post(new Runnable(){
@Override
public void run() {
try {
//Get my data and run createArticles();
} catch (Exception e) {
e.printStackTrace();
}
}
});
如果您想修改您的视图,例如显示您的文章,您必须 运行 UI/main 线程上的代码。为此,您可以在 Activity:
中调用 运行 runOnUiThread()
private void loadArticles(final String url) {
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
//Get my data ...
runOnUiThread(new Runnable() {
public void run() {
// modify Views here
createArticles();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
您需要使用函数runOnUiThread。这是用户界面线程的函数。
private void loadArticles(final String url) {
//Get my data ...
runOnUiThread(new Runnable() {
public void run() {
// modify Views here
createArticles();
}
});
});
我从我的 wordpress 网页加载我的文章。并想在我的应用程序中显示它们。 为了允许等待互联网响应,我必须创建一个线程。 使用此方法:
private void loadArticles(final String url) {
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
//Get my data and run createArticles();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
然后在 createArticles 函数中,我尝试使用我的函数 addViewToParent(...),但我收到一条错误消息:
Only the original thread that created a view hierarchy can touch its views.
我该如何解决这个问题?
编辑:如果重要的话,我会使用这些网络功能...
//Connect to the url
URL pageURL = new URL(url);
HttpURLConnection connection = (HttpURLConnection) pageURL.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream())
);
只需改用处理程序,这样您就可以访问 UI 主题:
new Handler().post(new Runnable(){
@Override
public void run() {
try {
//Get my data and run createArticles();
} catch (Exception e) {
e.printStackTrace();
}
}
});
如果您想修改您的视图,例如显示您的文章,您必须 运行 UI/main 线程上的代码。为此,您可以在 Activity:
中调用 运行runOnUiThread()
private void loadArticles(final String url) {
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
//Get my data ...
runOnUiThread(new Runnable() {
public void run() {
// modify Views here
createArticles();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
您需要使用函数runOnUiThread。这是用户界面线程的函数。
private void loadArticles(final String url) {
//Get my data ...
runOnUiThread(new Runnable() {
public void run() {
// modify Views here
createArticles();
}
});
});