在gridview中插入图像时获取stackoverflow异常

时间:2016-01-17 20:03:25

标签: android gridview android-viewpager viewpagerindicator

我有一个视图页面里面有gridview和图像(Emojis)在gridview里面的片段。它就像一个带有表情符号的活动,你可以在不同的表情符号类型之间滑动。 这是我的viewpager活动

SlidingTabLayout slidingTabLayout;
ViewPager viewPager;
Fragment[] fragments = {SmileysFragment.newInstance(0)};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.emoji_layout);
    slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tab_layout);
    viewPager = (ViewPager) findViewById(R.id.view_pager);

    initViews();
}

private void initViews() {
    FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            return fragments[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }
    };
    viewPager.setAdapter(adapter);
    List<DataHolder> dataHolders = new ArrayList<>();
    for (int i = 0; i < fragments.length; ++i) {
        dataHolders.add(new DataHolder(getResources().getDrawable(R.drawable.unselected),
                getResources().getDrawable(R.drawable.selected), "我", getResources().getColor(R.color.md_material_blue_600)));
    }
    slidingTabLayout.setUpViewPager(viewPager, dataHolders);
}

这是我的片段活动

 public class SmileysFragment extends Fragment {


GridView grid;
public static final String KEY_INDEX = "index";

int[] image_ids = new int[]{
        R.drawable.s1,
        R.drawable.s2,
        R.drawable.s3,
        R.drawable.s4,
        R.drawable.s5,
        R.drawable.s6,
        R.drawable.s6,
        R.drawable.s6,
        R.drawable.s6,
        R.drawable.s6,
        R.drawable.s6

};


public static SmileysFragment newInstance(int i) {
    Bundle args = new Bundle();
    args.putInt(KEY_INDEX, i);
    SmileysFragment fragment = new SmileysFragment();
    fragment.setArguments(args);
    return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view;
    if (container == null) {
        view =  inflater.inflate(R.layout.smiley_emoji_layout,container,false);
      grid = (GridView) view.findViewById(R.id.smileyGridview);
        SmileysAdapter adapter = new SmileysAdapter(getActivity(),      image_ids);
        grid.setAdapter(adapter);
    } else {
        view = container;
    }
    return view;
}

这是我的适配器类

public class SmileysAdapter extends BaseAdapter{

private Context mContext;
private int[] image_ids;
public SmileysAdapter(Context c,  int[] resources) {
    mContext = c;
    image_ids = resources;
}

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



@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return image_ids[position];
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid = convertView;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    if (convertView == null) {

        grid = inflater.inflate(R.layout.smiley_item_layout, null);


    }
    ImageView imageView = (ImageView) grid.findViewById(R.id.smiley_item);

    imageView.setImageResource(image_ids[position]);
    return grid;
}

这是我得到的例外

02:07:11 E AndroidRuntime : FATAL EXCEPTION: main

02:07:11 E AndroidRuntime : java.lang.StackOverflowError

02:07:11 E AndroidRuntime : at      android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5355)

 02:07:11 E AndroidRuntime : at   android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

 02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime:在android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

02:07:11 E AndroidRuntime : at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5359)

 02:07:11 W ActivityManager : Force finishing activity com.example.app/.EmojiActivity

请告诉我我做错了什么

1 个答案:

答案 0 :(得分:3)

你的onCreateView()搞砸了。

它应该是这样的:

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

        View view =  inflater.inflate(R.layout.smiley_emoji_layout, container, false);
        grid = (GridView) view.findViewById(R.id.smileyGridview);
        SmileysAdapter adapter = new SmileysAdapter(getActivity(), image_ids);
        grid.setAdapter(adapter);
        return view;
    }

我认为你对适配器getView()感到困惑。当片段创建其视图时,不涉及回收。容器永远不会为null。您应该始终返回已膨胀的视图,并且永远不会返回容器本身。