在Fragment中将适配器分配给GridView时为空指针

时间:2013-10-28 12:25:09

标签: android gridview android-fragments

为片段中的视图分配适配器时为空指针。

我有一个包含片段的活动。我想用GridView填充Fragment。 为此,我创建了一个自定义适配器 -

public class MyBaseAdapter extends BaseAdapter {

        private Context mContext;
        private List<ResolveInfo> mAppList;

        MyBaseAdapter(Context c, List<ResolveInfo> l) {
            mContext = c;
            mAppList = l;
        }
        public int getCount() {
            return mAppList.size();
        }
        public Object getItem(int position) {
            return mAppList.get(position);
        }
        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            ResolveInfo resolveInfo = mAppList.get(position);
            imageView.setImageDrawable(resolveInfo
                    .loadIcon(mPackageManager));
            return imageView;
        }
    }

我已将Fragment实现为 -

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.grid_of_app, container, false);

        HashMap<String, String> pkClsPair = new HashMap<String, String>();
        pkClsPair.put("com.android.contacts", "com.android.contacts.activities.PeopleActivity");
        pkClsPair.put("com.android.gallery", "com.android.camera.GalleryPicker");

        mPackageManager = getActivity().getPackageManager();
        List<ResolveInfo> intentList = null, intentList_temp = null;

        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        for (Map.Entry<String, String> entry : pkClsPair.entrySet()) {
            intent.setClassName(entry.getKey(), entry.getValue());
            if (intentList == null)
                intentList = getActivity().getPackageManager().queryIntentActivities(intent, 0);
            else {
                intentList_temp = (getActivity().getPackageManager().queryIntentActivities(intent, 0));
                intentList.addAll(intentList_temp);
            }
        }

        mBaseAdapter = new MyBaseAdapter(getActivity(), intentList);
        GridView mGridview = (GridView) getActivity().findViewById(R.id.gridView);
        mGridview.setAdapter(mBaseAdapter);

        mGridview.setOnItemClickListener(mOnItemClickListener);

        return rootView;
    }

但是,在将适配器 - mBaseAdapter - 分配给Gridview时,我似乎得到了NullPointer异常。据我所知它应该工作,因为我正在为适配器提供一个适当的上下文(我在调试时检查它,值不为空)。

提前致谢。

1 个答案:

答案 0 :(得分:2)

GridView mGridview = (GridView) getActivity().findViewById(R.id.gridView);

在上面换行getActivity()rootView