Android图片库和片段问题

时间:2013-12-11 14:14:47

标签: java android android-fragments android-gridview

我正在尝试使用fragment在我的应用中实施this图库。 我能够根据需要调整代码,但我得到NullPointerException,但我无法弄明白为什么......

问题与this完全一样,但是用户解决了这个问题而没有发布任何关于它的信息,所以我打开了这个问题....

这是我的代码:

GalleriaPersonaggio.java

import java.util.ArrayList;

import android.app.Fragment;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.italiandevteam.chuck.adapter.GridViewAdapter;
import com.italiandevteam.chuck.model.ImageItem;

public class GalleriaPersonaggio extends Fragment{

Integer personaggio = null;


    public GalleriaPersonaggio( int personaggio ){

        this.personaggio = personaggio;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        int id = getIdPersonaggio();

        View rootView = null;

        if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null)
                parent.removeView(rootView);
        }
        try {
            rootView = inflater.inflate(R.layout.gallery_grid, container, false);
        } catch (InflateException e) {
        }

        GridView gridView = (GridView) getView().findViewById(R.id.gridView);
        GridViewAdapter customGridAdapter = new GridViewAdapter(getActivity(), R.layout.gallery_row, getData(personaggio));
        gridView.setAdapter(customGridAdapter);

        return rootView;
    }

    public int getIdPersonaggio(){
        return this.personaggio;
    }

    private ArrayList getData(int personaggio) {
        final ArrayList imageItems = new ArrayList();
        // retrieve String drawable array
        TypedArray imgs = getResources().obtainTypedArray(R.array.chuck_ids);
        for (int i = 0; i < imgs.length(); i++) {
            Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
                    imgs.getResourceId(i, -1));
            imageItems.add(new ImageItem(bitmap, "Image#" + i));
        }

        return imageItems;

    }
}

这是我的GridViewAdapter.java

    import java.util.ArrayList;

    import android.app.Activity;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

    import com.italiandevteam.chuck.R;
    import com.italiandevteam.chuck.model.ImageItem;


    public class GridViewAdapter extends ArrayAdapter<ImageItem> {
        private Context context;
        private int layoutResourceId;
        private ArrayList<ImageItem> data = new ArrayList<ImageItem>();

        public GridViewAdapter(Context context, int layoutResourceId,
                        ArrayList<ImageItem> data) {
                super(context, layoutResourceId, data);
                this.layoutResourceId = layoutResourceId;
                this.context = context;
                this.data = data;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View row = convertView;
                ViewHolder holder = null;

                if (row == null) {
                        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                        row = inflater.inflate(layoutResourceId, parent, false);
                        holder = new ViewHolder();
                        holder.imageTitle = (TextView) row.findViewById(R.id.text);
                        holder.image = (ImageView) row.findViewById(R.id.image);
                        row.setTag(holder);
                } else {
                        holder = (ViewHolder) row.getTag();
                }

                ImageItem item = data.get(position);
                holder.imageTitle.setText(item.getTitle());
                holder.image.setImageBitmap(item.getImage());
                return row;
        }

        static class ViewHolder {
                TextView imageTitle;
                ImageView image;
        }
    }

and this is my ImageItem.java


import android.graphics.Bitmap;


public class ImageItem {
    private Bitmap image;
    private String title;

    public ImageItem(Bitmap image, String title) {
        super();
        this.image = image;
        this.title = title;
    }

    public Bitmap getImage() {
        return image;
    }

    public void setImage(Bitmap image) {
        this.image = image;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

这是LogCat:

12-11 15:03:30.262: E/AndroidRuntime(19554): FATAL EXCEPTION: main
12-11 15:03:30.262: E/AndroidRuntime(19554): java.lang.NullPointerException
12-11 15:03:30.262: E/AndroidRuntime(19554):    at com.italiandevteam.chuck.GalleriaPersonaggio.onCreateView(GalleriaPersonaggio.java:46)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at android.app.Fragment.performCreateView(Fragment.java:1695)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at android.os.Handler.handleCallback(Handler.java:730)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at android.os.Looper.loop(Looper.java:137)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at android.app.ActivityThread.main(ActivityThread.java:5103)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at java.lang.reflect.Method.invokeNative(Native Method)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at java.lang.reflect.Method.invoke(Method.java:525)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-11 15:03:30.262: E/AndroidRuntime(19554):    at dalvik.system.NativeStart.main(Native Method)

第46行是:

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

1 个答案:

答案 0 :(得分:1)

更改此

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

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

您需要使用膨胀的视图对象来初始化gridView

View rootView = rootView = inflater.inflate(R.layout.gallery_grid, container, false);   
GridView gridView = (GridView) rootView.findViewById(R.id.gridView);
GridViewAdapter customGridAdapter = new GridViewAdapter(getActivity(), R.layout.gallery_row, getData(personaggio));
gridView.setAdapter(customGridAdapter);