Android网格库视图错误

时间:2016-03-24 20:32:21

标签: java android android-studio gridview

我目前正在尝试使用基于网格的图库视图处理其中一个片段,并且我发现了这些错误

Error:(25, 20) error: onCreate(Bundle) in GalleryFragment cannot override onCreate(Bundle) in Fragment
attempting to assign weaker access privileges; was public
Error:(28, 9) error: cannot find symbol method setContentView(int)
Error:(30, 39) error: cannot find symbol method findViewById(int)
Error:(31, 43) error: incompatible types: GalleryFragment cannot be converted to Context

任何想法如何解决?我所遵循的教程似乎旨在将其作为一项活动,我不确定这是否有所作为。感谢

package com.test.test.app;


import android.content.Context;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class GalleryFragment extends Fragment
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView gridView = (GridView)findViewById(R.id.gridview);
        gridView.setAdapter(new MyAdapter(this));
    }

    private class MyAdapter extends BaseAdapter
    {
        private List<Item> items = new ArrayList<Item>();
        private LayoutInflater inflater;

        public MyAdapter(Context context)
        {
            inflater = LayoutInflater.from(context);

            items.add(new Item("Image 1", R.drawable.image_1));
            items.add(new Item("Image 2", R.drawable.image_2));
            items.add(new Item("Image 3", R.drawable.image_3));
            items.add(new Item("Image 4", R.drawable.image_4));
            items.add(new Item("Image 5", R.drawable.image_5));
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int i)
        {
            return items.get(i);
        }

        @Override
        public long getItemId(int i)
        {
            return items.get(i).drawableId;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup)
        {
            View v = view;
            ImageView picture;
            TextView name;

            if(v == null)
            {
                v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
                v.setTag(R.id.picture, v.findViewById(R.id.picture));
                v.setTag(R.id.text, v.findViewById(R.id.text));
            }

            picture = (ImageView)v.getTag(R.id.picture);
            name = (TextView)v.getTag(R.id.text);

            Item item = (Item)getItem(i);

            picture.setImageResource(item.drawableId);
            name.setText(item.name);

            return v;
        }

        private class Item
        {
            final String name;
            final int drawableId;

            Item(String name, int drawableId)
            {
                this.name = name;
                this.drawableId = drawableId;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

Fragment未提供context。因此你不能使用它:

gridView.setAdapter(new MyAdapter(this));

应该是:

gridView.setAdapter(new MyAdapter(getActivity()));

注意:getActivityonCreate onCreateView Fragment获取活动上下文并不安全,因为activity可能尚未创建。尝试从onActivityCreated调用它。

答案 1 :(得分:0)

您可以将代码从片段移动到活动,也可以将onCreate替换为:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_main, container, false);
    GridView gridView = (GridView) view.findViewById(R.id.gridview);
    gridView.setAdapter(new MyAdapter(getActivity()));
    return view;
}