[英]setText on UIThread not working
在我的AsyncTask中
@Override
protected void onProgressUpdate(Void... values)
方法,该方法在UI线程上运行,我尝试将MainLayout的Text设置为“”:
LayoutInflater temporaryInflater = (LayoutInflater) myActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View temporaryRootView = temporaryInflater.inflate(R.layout.myLayout, (ViewGroup) gv.getParent(), false);
EditText temporaryEditText = (EditText) temporaryRootView.findViewById(R.id.myEdtiText);
temporaryEditText.setText("");
这应该可以正常工作,我尝试更改UIThread上的视图,我提供了一个带有rootViewGroup父级的正确RootView以提供LayoutParams,但是什么也没有发生。 EditText与旧的Text保持在一起。
有什么建议么? 将不胜感激。
关于Rohan550的评论/提示的更新 :
我将尝试您的解决方案,但并非完全相同(仅用于理解):
public void resetEditText() {
EditText temporaryEditText = (EditText) MyActivity.this.findViewById(R.id.myEditText);
temporaryEditText.setText("");
}
我在onProgressUpdate中的Activity和UIThread的AsyncTask中调用:
MyActivity wusch = new MyActivity();
wusch.resetEditText();
但是它将在resetEditText
方法中的findViewById
处引发NPE。
为什么?
尝试更改:temporalSearchbar.setText(“”); 临时文件.setText(“”);
如果添加一些库,则AsyncTask
可能会损坏。 例如admob或flurry。 不要假设onProgressUpdate
将在uithread中,即使它的功能是那样。 因此,请尝试使用Handler。 在onCreate中创建一个处理程序,然后在onProgressUpdate
使用它。
Handler h = new Handler(); // in activity on create
然后
@Override
protected void onProgressUpdate(Void... values){
h.post(new Runnable() {
@Override
public void run() {
// do ui operations
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.