在 gridview 中插入图像时出现 stackoverflow 异常
Getting stackoverflow exception while inserting images in gridview
我在其中有一个视图页面,其中有带有 gridview 的片段和 gridview 中的图像(表情符号)。它就像带有表情符号的 activity,您可以在不同的表情符号类型之间滑动。
这是我的 viewpager activity
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);
}
这是我的片段activity
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;
}
这是我的适配器class
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.WhosebugError
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
请告诉我我做错了什么
你的 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()
感到困惑。当片段创建它的视图时,不涉及回收。容器永远不会为空。你应该总是 return 你膨胀的视图,而不是 return 容器本身。
我在其中有一个视图页面,其中有带有 gridview 的片段和 gridview 中的图像(表情符号)。它就像带有表情符号的 activity,您可以在不同的表情符号类型之间滑动。 这是我的 viewpager activity
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);
}
这是我的片段activity
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;
}
这是我的适配器class
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.WhosebugError
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
请告诉我我做错了什么
你的 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()
感到困惑。当片段创建它的视图时,不涉及回收。容器永远不会为空。你应该总是 return 你膨胀的视图,而不是 return 容器本身。