我试图将“entityFinal”传递给run()方法,但由于它是可运行的,所以我不能有任何参数。
runOnUiThread 上的“responseView.setText”被注释,因为我的 android studio 总是打印我的responseView为空,而且我不明白为什么会这样,因为我正在使用正确查看 xml。
有人可以在这里给我提示吗?我测试了很多东西,asyncTask,多线程,但这是我“几乎”工作的唯一方法。
提前谢谢您。
xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Blank"
android:id="@+id/textView"
android:layout_below="@+id/button2"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"
android:layout_marginTop="50dp"/>
Java文件代码:
final TextView responseView = (TextView) activity.findViewById(R.id.textView);
new Thread(){
public void run(){
try{
HttpResponse httpResponse = httpclient.execute(httppost);
final HttpEntity entity = httpResponse.getEntity();
final String entityFinal = entity.getContent().toString();
}catch(IOException e){
System.out.println(e.getMessage());
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
//responseView.setText(entityFinal);
System.out.println(responseView);
}
});
}
}.start();
最佳答案
就你的情况而言,移动
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
//responseView.setText(entityFinal);
System.out.println(responseView);
}
);
进入try
block 应该就足够了。一般来说,您始终可以使用一个类来实现 Runnable,并使用带有任意数量/类型参数的构造函数。例如
public class MyRunnable implements Runnable {
private final String mString;
public MyRunnable(String string) {
mString = string;
}
@Override
public void run() {
System.out.println(mString);
}
}
关于java - 可运行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26994488/