Android stackoverflow错误寻呼机适配器

时间:2013-06-25 09:57:36

标签: java android viewgroup stack-overflow android-pageradapter

大家好! 我正在尝试制作类似这样的程序:http://www.androidbegin.com/tutorial/android-viewpager-gallery-images-and-texts-tutorial/ 但是当我运行程序时,我总是得到以下错误代码:

 06-25 09:51:44.507: E/AndroidRuntime(721): FATAL EXCEPTION: main 06-25
 09:51:44.507: E/AndroidRuntime(721): java.lang.StackOverflowError
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.View.onWindowVisibilityChanged(View.java:4042) 06-25
 09:51:44.507: E/AndroidRuntime(721):   at
 android.view.View.dispatchAttachedToWindow(View.java:6159) 06-25
 09:51:44.507: E/AndroidRuntime(721):   at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1122)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at
 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
 06-25 09:51:44.507: E/AndroidRuntime(721):     at android.

我的PagerAdapter类:

public class LoafMakingPageAdapter extends PagerAdapter {

    Context context;
    int[] pictures;
    LayoutInflater inflater;

    public LoafMakingPageAdapter(Context context,int[] pictures){
        this.context = context;
        this.pictures = pictures;
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        ImageView imgLoaf;
        TextView textLoaf;

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.loafmaking_items, container);

        textLoaf = (TextView) itemView.findViewById(R.id.loaf_text);

        imgLoaf = (ImageView) itemView.findViewById(R.id.loaf_image);
        imgLoaf.setImageResource(pictures[position]);

        container.addView(itemView);

        return itemView;
    }

}

可能是什么问题以及如何解决?

1 个答案:

答案 0 :(得分:4)

试试这个

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.loafmaking_items, false);

工作解决方案:

按照Slenkra

的评论中的建议
View itemView = inflater.inflate(R.layout.loafmaking_items, container, false);