开发Gallery视图时setAdapter出错

时间:2016-09-12 16:50:55

标签: java android android-fragments nullpointerexception

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

XML FILE(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>

片段文件(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;
    }

}

适配器类(Image Adapter.java):

public class ImageAdapter extends BaseAdapter{
    private Context context;

    public Integer[] images = {
            R.drawable.about, R.drawable.gallery,
            R.drawable.home, R.drawable.invitation,
            R.drawable.register, R.drawable.membership,
            R.drawable.about, R.drawable.gallery,
            R.drawable.home, R.drawable.invitation,
            R.drawable.register, R.drawable.membership,
            R.drawable.about, R.drawable.gallery,
            R.drawable.home, R.drawable.invitation,
            R.drawable.register, R.drawable.membership,
            R.drawable.about, R.drawable.gallery
    };

    public ImageAdapter(Context c){
        context = c;
    }

    public ImageAdapter(gallery g){

    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object getItem(int i) {
        return images[i];
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(images[i]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setLayoutParams(new GridView.LayoutParams(240,240));
        return imageView;
    }
}

这是我收到的错误:

09-12 22:25:45.359 29157-29157/com.example.tonymathew.eliteenglishclub E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tonymathew.eliteenglishclub, PID: 29157
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
 at com.example.tonymathew.eliteenglishclub.gallery.onCreateView(gallery.java:28)
 at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
 at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
 at android.os.Handler.handleCallback(Handler.java:746)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5443)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

3 个答案:

答案 0 :(得分:3)

更改

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

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

使用片段时使用 getActivity()代替

希望这会对你有所帮助

答案 1 :(得分:1)

在片段文件中,您应该从

更改
  

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

  

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

答案 2 :(得分:0)

在您的片段活动中,您需要使用getActivity():

gridView.setAdapter(new ImageAdapter(getActivity()))

使用getActivity()时要小心,因为有时它会返回null。有关详细信息,请参阅此主题:android get activity returns null