Fragment中的onPostExecute无效(在Activity中有效)

时间:2013-11-29 20:52:57

标签: android gridview android-fragments findviewbyid

我的问题:此代码在我的活动中不起作用我正在尝试实现从parse.com获取数据的gridView适配器

@Override
    protected void onPostExecute(Void result) {
        // Locate the gridview in gridview_main.xml
        gridview = (GridView) findViewById(R.id.gridview);
        // Pass the results into ListViewAdapter.java
        adapter = new GridViewAdapter(MainActivity.this,
                phonearraylist);
        // Binds the Adapter to the ListView
        gridview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}
}

这是我的“DiscoverActivity”课程,它出自SherlockFragment

公共类DiscoverActivity扩展了SherlockFragment

// Declare Variables
GridView gridview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ImageAdapter adapter;
private List<ImageList> phonearraylist = null;

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.activity_discover,container,false);
//        GridView gridView = (GridView) view.findViewById(R.id.gridview);
 //        gridView.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity().
    new RemoteDataTask().execute();
    return view;
}

private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

//            Toast.makeText(getActivity(), "hello",Toast.LENGTH_LONG).show();
        Toast.makeText(getActivity().getApplicationContext(),"Loading...",Toast.LENGTH_LONG).show();

    }

protected Void doInBackground(Void... params) {
    // Create the array
    phonearraylist = new ArrayList<ImageList>();
    try {
        // Locate the class table named "SamsungPhones" in Parse.com
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                "testData2");
        // Locate the column named "position" in Parse.com and order list
        // by ascending
        query.orderByAscending("Price");
        ob = query.find();
        for (ParseObject country : ob) {
            ParseFile image = (ParseFile) country.get("Photo");
            ImageList map = new ImageList();
            map.setPhone(image.getUrl());
            phonearraylist.add(map);
        }
    } catch (ParseException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return null;
}

//Here is what I have tried so far...
@Override
protected void onPostExecute(Void result) {
    // Locate the gridview in gridview_main.xml
    gridview = (GridView) findViewById(R.id.gridview);
    // Pass the results into ListViewAdapter.java
    //adapter = new ImageAdapter(DiscoverActivity.this,
      //      phonearraylist);
    GridView gridView = (GridView) view.findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity().
    // Binds the Adapter to the ListView
    gridview.setAdapter(adapter);
    // Close the progressdialog
    mProgressDialog.dismiss();
}
}
}

我在onPostExecute

中找不到findViewById

提前感谢你们给我的任何帮助!

2 个答案:

答案 0 :(得分:0)

你有没有在onCreate方法中写这个语句? (活动)

setContentView(R.layout.some_activity);

然后你可以使用那里定义的所有id。

答案 1 :(得分:0)

在您引用的示例中,父类是Activity,其父类findViewById()没有方法SherlockFragment。因此,您需要使用gridView中膨胀的布局(正如您所做的那样)获取对onCreate()的引用,我假设GridView位于布局中。

因为您的AsyncTask是一个内部类,它可以访问外部类(您的片段类)的实例变量,所以您可以在GridView中创建指向onCreate()的指针并使用它在onPostExecute()