Android doInBackground中的gridview出现异步任务异常

Android doInBackground中的gridview出现异步任务异常,android,android-asynctask,android-gridview,Android,Android Asynctask,Android Gridview,我正在片段中使用gridview。我正在doInBackground中加载gridview。但是,我得到AsyncTask#1致命异常。下面是我尝试过的代码: public class Home extends Fragment implements TabHost.OnTabChangeListener, OnClickListener { TabHost tabHost; View vi; StaggeredAdapter adapter;

我正在片段中使用gridview。我正在doInBackground中加载gridview。但是,我得到AsyncTask#1致命异常。下面是我尝试过的代码:

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() {


        }

    }

}
公共类Home扩展片段实现TabHost.OnTabChangeListener,
onclick侦听器{
TabHost-TabHost;
视图六;
交错适配器;
文本视图电视;
公共字符串URL[]={
"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" };
公共静态最终字符串ARG\u SECTION\u NUMBER=“SECTION\u NUMBER”;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
vi=充气机。充气(R.布局图.主,容器,假);
按钮iv=(按钮)vi.findViewById(R.id.btnMoreDialog);
iv.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
PopupMenu homepopup=新的PopupMenu(getActivity(),v);
MenuInflater充气器=homepopup.getMenuInflater();
充气器。充气(R.menu.moredilog,homepopup.getMenu());
homepopup.show();
}
});
新对话框().execute(URL);
返回vi;
}
私有类对话框扩展了异步任务
{
@凌驾
受保护的Void doInBackground(字符串[]…参数){
交错EdgridView网格视图=(交错EdgridView)vi
.findViewById(R.id.StaggedGridView1);
int margin=getResources().getDimensionPixelSize(R.dimen.margin);
setItemMargin(margin);
setPadding(边距,0,边距,0);
//交错适配器=新交错适配器(main activity.this,
//R.id.photoimageview、URL、R.layout.row\u\u demo);
StaggeredAdapter=新的StaggeredAdapter(vi.getContext(),
R.id.photoimageview,参数[0],R.layout.row\u交错\u demo);
setAdapter(适配器);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
}
@凌驾
受保护的void onPreExecute(){
}
}
}
有什么想法吗?
谢谢,

您正试图在
doInbackground
中更新ui,这是不可能的

       gridView.setAdapter(adapter);
您需要在ui线程上更新ui

使用
runOnUiThread
或在
onPostExecute

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

                 }
                 });

发布堆栈跟踪。我尝试了你的建议,但我发现应用程序可能在主线程上做了太多的工作。gridview不显示,该屏幕上的popmenu也不工作。这意味着您的应用程序在主ui线程上做了太多工作。您必须在线程中移动一些任务。你做得不对。上面发布的代码是我的屏幕代码。我在那个页面上有三个按钮和gridview。单击其中一个按钮,我将显示一个弹出菜单和加载的gridview。该应用程序没有给出任何错误。您可以帮助我如何执行该操作吗。@asifa从url下载图像的代码部分在哪里