0

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?

MMike
  • 598
  • 3
  • 23
  • Is your `asyncTask` is inside your `Activity` ? – Mohammad Rahchamani Nov 29 '14 at 11:49
  • have you try to move your code to publishProgress from onProgressUpdate ? – Haresh Chhelana Nov 29 '14 at 11:49
  • @HareshChhelana Especially it already runs on the UIThread because if not my app would Crash with a Exception. There must be something different wrong. – MMike Nov 29 '14 at 11:50
  • @MohammadRahchamani No it's not an anonyoums class. – MMike Nov 29 '14 at 11:50
  • @MMike,what is gv.getParent() ? can you please post full or more code ? – Haresh Chhelana Nov 29 '14 at 11:52
  • @HareshChhelana No i didn't try this but publishProgress just calls onProgressUpdate. So where's the difference? – MMike Nov 29 '14 at 11:53
  • I'm assuming your UI is already inflated and visible at this point. What you seem to be doing is reinflating the UI without setting it as root-view. Instead you should do something like: myActivity.getView().findViewById().setText() – RhodanV5500 Nov 29 '14 at 11:54
  • Check : http://developer.android.com/reference/android/os/AsyncTask.html#onProgressUpdate(Progress...) – Haresh Chhelana Nov 29 '14 at 11:54
  • @RhodanV5500 can you explain this a Little bit more. You are correct. The Layout is view but my AsyncTask is a seperate Class so it has no Access to the Views of my Activity. And to be honest a really don't like to put the whole activity into another class.. Is there another cleaner way? – MMike Nov 29 '14 at 11:57
  • @HareshChhelana That's correct but These textChange-Code in my Questions only runs on the onProgressUpdate, so it already has been invoked. Maybe i misunderstand something.. – MMike Nov 29 '14 at 11:59
  • @MMike The inflater is a class that converts a XML layout into a new view. You have to set this view to your activity if you want it to become visible or it will be lost. So just reinflating is not what you need. To separate your activity and async task properly you could add a listener to your task or use notifications. There are several other approaches, but these are the ones I would usually go with. – RhodanV5500 Nov 29 '14 at 12:05
  • @RhodanV5500 would you declarate it as clean if i just put the EditText from the Activity to the AsyncTask. This should do the trick but is it clean? – MMike Nov 29 '14 at 12:14
  • @MMike the accepted answer of this post is what I would do: http://stackoverflow.com/questions/9963691/android-asynctask-sending-callbacks-to-ui – RhodanV5500 Nov 29 '14 at 12:19
  • @RhodanV5500 ok i will have a look at that. But i updated my question, isn't that the same?? – MMike Nov 29 '14 at 12:36
  • @MMike it's not the same. By calling MyActivity wusch = new MyActivity(); are creating a new instance of your activity. OnCreateView is not called, in this case so there is no root view. Therefore you get a null pointer exception when calling wusch.resetEditText();. I would subclass AsyncTask and add a listener. The activity can implement this listener and update the UI. – RhodanV5500 Nov 29 '14 at 13:04
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/65870/discussion-on-question-by-mmike-settext-on-uithread-not-working). – Taryn Nov 29 '14 at 13:06

2 Answers2

0

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

Deepak Singh
  • 144
  • 6
0

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
            }
        });
}
Adem
  • 9,402
  • 9
  • 43
  • 58
  • Thank you for your Response. But, my app isn't crashing. If onProgressUpdate wouldn't run of the UIThread and i would try to Change a View, what i do, then it must throw an exception. So Rhodan550's hint sounds much more logically – MMike Nov 29 '14 at 12:28