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>
答案 0 :(得分:2)
GridView gridview = (GridView)getActivity().findViewById(R.id.grid_view);
您必须使用不getActivity()
的视图来呼叫findViewById
GridView gridview = (GridView)view.findViewById(R.id.grid_view);
因为您的GridView
属于您正在膨胀的Layout