1

大家好!我正在尝试制作一个类似的程序: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;
    }

}

可能是什么问题,我该如何解决?

4

1 回答 1

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);
于 2013-06-25T09:59:21.980 回答