简体   繁体   中英

setText on UIThread not working

in my AsyncTask in the

@Override
protected void onProgressUpdate(Void... values)

method, which runs on the UI-Thread i try to set Text of my MainLayout to "":

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("");

This should work fine, i try to Change a view on the UIThread i provide a correct RootView with a rootViewGroup parent to provide LayoutParams but nothing happens. EditText stays with the old Text.

Any suggestions? Would appreciate that.

Update regarding Rohan550's comments/hint :

I will try your solution but isn't that the same(just for understanding):

public void resetEditText() {
    EditText temporaryEditText = (EditText) MyActivity.this.findViewById(R.id.myEditText);
    temporaryEditText.setText("");
}

which is in the Activity and in the AsyncTask on the UIThread in onProgressUpdate i call:

MyActivity wusch = new MyActivity();
wusch.resetEditText();

But it throws a NPE at findViewById in the resetEditText method..

Why?

Try to change: temporarySearchbar.setText(""); to temporaryEditText .setText("");

AsyncTask can be broken if you add some library. for instance, admob or flurry. dont assume onProgressUpdate will be in uithread even its feature is that. so, try to use Handler. create a handler in onCreate, then use it in onProgressUpdate this.

Handler h = new Handler(); // in activity on create

then

@Override
protected void onProgressUpdate(Void... values){
h.post(new Runnable() {
            @Override
            public void run() {
                // do ui operations
            }
        });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM