0

MainFragment 类:(即使在膨胀之后,gridview 也是空的)

public class FragmentMain extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        GridView gridview = (GridView)getActivity().findViewById(R.id.grid_view);
        gridview.setAdapter(new ImageAdapter(getActivity(), thumbnails)); //gridview is null!!!

        getActivity().setTitle(title);
        return view;
    }
}

fragment_main.xml 布局

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:columnWidth="90dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center"
    android:stretchMode="columnWidth" >  

</GridView>
4

1 回答 1

2
 GridView gridview = (GridView)getActivity().findViewById(R.id.grid_view);

你必须使用视图不getActivity()调用findViewById

 GridView gridview = (GridView)view.findViewById(R.id.grid_view);

因为你GridView属于Layout你,所以你在膨胀

于 2013-06-23T10:49:36.050 回答