2

I'm using a XML Parsing in doInBackground (for Load Screen) and then set it with a LazyAdapter in the listview(list).

First the Codesample:

public class NewCoupons extends Activity {

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
             
        bar = (ProgressBar) this.findViewById(R.id.bar);
        new ProgressTask().execute();
}

private class ProgressTask extends AsyncTask <Void,Void,Void>{

        @Override
        protected Void doInBackground(Void... arg0) {   
          .....
          list=(ListView)findViewById(R.id.list);
          adapter = new LazyAdapter(this, xy);        
          list.setAdapter(adapter);
        }
}
}

When I user "NewCoupons.this" I become:

05-15 21:01:16.553: E/AndroidRuntime(6319): FATAL EXCEPTION: AsyncTask

1 05-15 21:01:16.553: E/AndroidRuntime(6319): java.lang.RuntimeException: An error occured while executing

doInBackground() 05-15 21:01:16.553: E/AndroidRuntime(6319): at

android.os.AsyncTask$3.done(AsyncTask.java:299) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)

05-15 21:01:16.553: E/AndroidRuntime(6319): at

java.util.concurrent.FutureTask.setException(FutureTask.java:219)

05-15 21:01:16.553: E/AndroidRuntime(6319): at

java.util.concurrent.FutureTask.run(FutureTask.java:239) 05-15

21:01:16.553: E/AndroidRuntime(6319): at

java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)

05-15 21:01:16.553: E/AndroidRuntime(6319): at

java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)

05-15 21:01:16.553: E/AndroidRuntime(6319): at

java.lang.Thread.run(Thread.java:856) 05-15 21:01:16.553:

E/AndroidRuntime(6319): Caused by:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the

original thread that created a view hierarchy can touch its views.

05-15 21:01:16.553: E/AndroidRuntime(6319): at

android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746) 05-15

21:01:16.553: E/AndroidRuntime(6319): at

android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823) 05-15

21:01:16.553: E/AndroidRuntime(6319): at

android.view.View.requestLayout(View.java:15473) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

android.view.View.requestLayout(View.java:15473) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

android.view.View.requestLayout(View.java:15473) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

android.view.View.requestLayout(View.java:15473) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

android.view.View.requestLayout(View.java:15473) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

android.widget.AbsListView.requestLayout(AbsListView.java:1819) 05-15

21:01:16.553: E/AndroidRuntime(6319): at

android.widget.ListView.setAdapter(ListView.java:490) 05-15

21:01:16.553: E/AndroidRuntime(6319): at

com.example.androidhive.NewCoupons$ProgressTask.doInBackground(NewCoupons.java:170)

05-15 21:01:16.553: E/AndroidRuntime(6319): at

com.example.androidhive.NewCoupons$ProgressTask.doInBackground(NewCoupons.java:1)

05-15 21:01:16.553: E/AndroidRuntime(6319): at

android.os.AsyncTask$2.call(AsyncTask.java:287) 05-15 21:01:16.553:

E/AndroidRuntime(6319): at

java.util.concurrent.FutureTask.run(FutureTask.java:234) 05-15

21:01:16.553: E/AndroidRuntime(6319): ... 3 more

The Problem: is that I cant use "this" on new LazyAdapter, that would be a error. What I must set for "this" in new LazyAdapter?

Thanks.

Community
  • 1
  • 1
user1878413
  • 1,813
  • 4
  • 18
  • 24

2 Answers2

2

You have to pass in a context. Means, you can do this: MyClassName.this or getActivity(), if you are in a fragment.

Edit:

Here is your error:

Only the original thread that created a view hierarchy can touch its views.

Community
  • 1
  • 1
Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • Thank you. What can I do to repair the error? When im create the list in the doInBackground I cant say: Send the Data from doInBackground to onCreate and fill the listview. – user1878413 May 15 '13 at 21:16
  • 1
    You have to do all UI stuff on the UI thread. The method on `onPostExecute()` from the Asynctask has access to the UI thread, so set the your images there. – Ahmad May 15 '13 at 21:19
  • And how I can send the _ArrayList> **newCoupons** = new ArrayList>();_ ArrayList from doItBackground() to onPostExecute() – user1878413 May 15 '13 at 21:36
  • Great. He do it all but dont going in the LazyAdapter. I dont become a error bt when i test it with a System.out.println in the LazyAdapter.java - i dont become it. Do you have a idea? – user1878413 May 15 '13 at 21:56
1

If your activity's called MainActivity use MainActivity.this

Dmitry Guselnikov
  • 1,048
  • 1
  • 8
  • 21