想要在片段中构建图像gridview,但是当此片段处于活动状态时应用程序崩溃。 教程是this。 MainActivity打开以下内容。其他文件是复制粘贴:
public class ImageActivity extends Fragment {
private GridView gridView;
private GridViewAdapter gridAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_wallpaper, container, false);
gridView = (GridView) rootView.findViewById(R.id.gridView);
gridAdapter = new GridViewAdapter(getActivity(), R.layout.fragment_wallpaper_item, getData());
gridView.setAdapter(gridAdapter);
return rootView;
}
// Prepare some dummy data for gridview
private ArrayList<ImageItem> getData() {
final ArrayList<ImageItem> imageItems = new ArrayList<>();
TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
for (int i = 0; i < imgs.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgs.getResourceId(i, -1));
imageItems.add(new ImageItem(bitmap, "Image#" + i));
}
return imageItems;
}}