0

I am using a gridview in fragment. I am loading the gridview in doInBackground.However, I get AsyncTask#1 Fatal Exception. Below is the code that i have tried:

public class Home extends Fragment implements TabHost.OnTabChangeListener,
        OnClickListener {

    TabHost tabHost;
    View vi;
    StaggeredAdapter adapter;
    TextView tv;
    public String urls[] = {

    "http://farm9.staticflickr.com/8462/8005636463_0cb4ea6be2.jpg",
            "http://farm8.staticflickr.com/7232/6913504132_a0fce67a0e_c.jpg",
            "http://farm5.staticflickr.com/4133/5096108108_df62764fcc_b.jpg",
            "http://farm5.staticflickr.com/4074/4789681330_2e30dfcacb_b.jpg",
            "http://farm9.staticflickr.com/8208/8219397252_a04e2184b2.jpg",
            "http://farm9.staticflickr.com/8483/8218023445_02037c8fda.jpg",
            "http://farm9.staticflickr.com/8335/8144074340_38a4c622ab.jpg",
            "http://farm9.staticflickr.com/8060/8173387478_a117990661.jpg",
            "http://farm9.staticflickr.com/8056/8144042175_28c3564cd3.jpg",
            "http://farm9.staticflickr.com/8183/8088373701_c9281fc202.jpg",
            "http://farm9.staticflickr.com/8185/8081514424_270630b7a5.jpg",
            "http://farm9.staticflickr.com/8462/8005636463_0cb4ea6be2.jpg",
            "http://farm9.staticflickr.com/8306/7987149886_6535bf7055.jpg",
            "http://farm9.staticflickr.com/8444/7947923460_18ffdce3a5.jpg",
            "http://farm9.staticflickr.com/8182/7941954368_3c88ba4a28.jpg",
            "http://farm9.staticflickr.com/8304/7832284992_244762c43d.jpg",
            "http://farm9.staticflickr.com/8163/7709112696_3c7149a90a.jpg",
            "http://farm8.staticflickr.com/7127/7675112872_e92b1dbe35.jpg",
            "http://farm8.staticflickr.com/7111/7429651528_a23ebb0b8c.jpg",
            "http://farm9.staticflickr.com/8288/7525381378_aa2917fa0e.jpg",
            "http://farm6.staticflickr.com/5336/7384863678_5ef87814fe.jpg",
            "http://farm8.staticflickr.com/7102/7179457127_36e1cbaab7.jpg",
            "http://farm8.staticflickr.com/7086/7238812536_1334d78c05.jpg",
            "http://farm8.staticflickr.com/7243/7193236466_33a37765a4.jpg",
            "http://farm8.staticflickr.com/7251/7059629417_e0e96a4c46.jpg",
            "http://farm8.staticflickr.com/7084/6885444694_6272874cfc.jpg" };

    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        vi = inflater.inflate(R.layout.home, container, false);
        Button iv = (Button) vi.findViewById(R.id.btnMoreDialog);
        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu homepopup = new PopupMenu(getActivity(), v);

                MenuInflater inflater = homepopup.getMenuInflater();

                inflater.inflate(R.menu.moredialog, homepopup.getMenu());

                homepopup.show();

            }
        });


        new Dialogs().execute(urls);
        return vi;
    }

    private class Dialogs extends AsyncTask<String[] , Void, Void>
    {
        @Override
        protected Void doInBackground(String[]... params) {
            StaggeredGridView gridView = (StaggeredGridView) vi
                    .findViewById(R.id.staggeredGridView1);

            int margin = getResources().getDimensionPixelSize(R.dimen.margin);
            gridView.setItemMargin(margin);
            gridView.setPadding(margin, 0, margin, 0);

            // StaggeredAdapter adapter = new StaggeredAdapter(MainActivity.this,
            // R.id.photoimageview, urls, R.layout.row_staggered_demo);

            StaggeredAdapter adapter = new StaggeredAdapter(vi.getContext(),
                    R.id.photoimageview, params[0], R.layout.row_staggered_demo);

            gridView.setAdapter(adapter);

           return null;
       }


        @Override
        protected void onPostExecute(Void result) {


        }
        @Override
        protected void onPreExecute() {


        }

    }

}

Any ideas pls? Thanks,

Shyam
  • 6,376
  • 1
  • 24
  • 38
asifa
  • 771
  • 1
  • 28
  • 63

1 Answers1

1

You are attempting to update ui in doInbackground which is not possible.

       gridView.setAdapter(adapter);

You need to update ui on the ui thread.

Use runOnUiThread or update ui in onPostExecute

      getActivity().runOnUiThread(new Runnable() //run on ui thread
                 {
                  public void run() 
                  { 

                 }
                 });
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • I tried your suggestions but i am getting the application may be doing too much work on the main thread. The gridview is not displayed and the popmenu on that screen is also not working. – asifa Jul 03 '13 at 13:52
  • that means your app is doing too much work on the main ui thread. you will have to move some tasks inside a thread. you are not doing it right. – Raghunandan Jul 03 '13 at 13:54
  • The above posted code is my screen code. I have three buttons and gridview on that page. On the click of one of the buttons i am displaying a popup menu and the gridview on load.The app does not give any errors – asifa Jul 03 '13 at 13:55
  • Pls can u help me with how to do it. – asifa Jul 03 '13 at 14:09
  • @asifa where is the part of the code that downloads images from the url – Raghunandan Jul 03 '13 at 14:12
  • it is part of the staggered gridview library. Pls check http://pastie.org/8106902 – asifa Jul 03 '13 at 14:30
  • @asifa i have not used staggered gridview library. So i can't help much on that. – Raghunandan Jul 03 '13 at 14:32
  • pls atleast check the pastie all the images are referring to that file – asifa Jul 03 '13 at 14:32