小编Ton*_*hew的帖子

开发Gallery视图时setAdapter出现错误

基本上,我正在尝试创建一个具有图片库视图的应用程序。我使用网格视图进行了画廊视图。使用片段,而不是活动。但是,打开图库会话时,应用程序崩溃。这是代码:

XML文件(fragment_gallery.xml):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    tools:context="com.example.tonymathew.eliteenglishclub.gallery">

    <!-- TODO: Update blank fragment layout -->

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gridView"
        android:columnWidth="100dp"
        android:stretchMode="columnWidth"

        android:numColumns="auto_fit"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="5dp"
        android:layout_gravity="center" />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

片段文件(gallery.java):

public class gallery extends Fragment {
    GridView gridView;

    public gallery() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_gallery, container, false);

        gridView = (GridView) container.findViewById(R.id.gridView);

        gridView.setAdapter(new ImageAdapter(this));

        return view;
    }

}
Run Code Online (Sandbox Code Playgroud)

适配器类(Image Adapter.java):

public class ImageAdapter extends BaseAdapter{
    private Context context;

    public Integer[] images …
Run Code Online (Sandbox Code Playgroud)

java android nullpointerexception android-fragments

5
推荐指数
1
解决办法
320
查看次数