从活动转移到ViewPager失败

时间:2017-10-12 20:50:07

标签: java android

我在 SecondActivity

上有按钮
Button b1 = findViewById(R.id.button1);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override


            public void onClick(View view) {
                Intent i= new Intent(SecondActivity.this,ThirdActivity.class);

               startActivity(i);

            }
        });

ThirdActivity 我有这个:

public class ThirdActivity extends Activity {

    ImageView img2;
    ViewPager viewpager;
    private int[] normal = {R.drawable.ii1,R.drawable.i2,R.drawable.i3,R.drawable.i4};
    ArrayList<Product2> listnormal = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        viewpager = (ViewPager) findViewById(R.id.view_pager);

        for(int i= 0; i< normal.length; i++){
            listnormal.add(new Product2(i,normal[i]));

        }

        swipePage swipe = new swipePage(this,listnormal);

        viewpager.setAdapter(swipe);

    }


    class swipePage extends PagerAdapter{

        LayoutInflater layoutInflater;
        private Context context;
        ArrayList<Product2> list= new ArrayList<>();

        public swipePage(Context context, ArrayList<Product2> list) {
            this.context = context;
            this.list = list;
        }


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

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

        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            layoutInflater= (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            View view = layoutInflater.inflate(R.layout.item_pager,container,false);
            ImageView bigimg = view.findViewById(R.id.imageViewbig);
            TextView numtxt = view.findViewById(R.id.textViewP5);
            bigimg.setImageResource(list.get(position).getImgpages());
            numtxt.setText(list.get(position).getNum());
            container.addView(view);

            return view;
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((LinearLayout)object);

        }
    }
}

问题是,当我点击按钮将我移至ThirdActivity时,会显示一条消息很遗憾我的应用已停止,当我删除了除此代码之外的ThirdActivity上的代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);}

它完美无缺。它让我没有任何问题地进入下一个活动。 我不知道究竟是什么问题。 我没有使用任何片段,如果有特殊意图请告诉我或暗示我。

Logcat错误

10-13 00:00:42.653 965-965/? E/installd: eof
10-13 00:00:42.653 965-965/? E/installd: failed to read size

0 个答案:

没有答案