我试图将viewPager添加到我的项目中,但是有问题

该问题是由以下原因引起的:

java.lang.ArrayIndexOutOfBoundsException:       长度= 0;索引= 0      在com.example.proje.DetayActivity.onCreate(DetayActivity.java:78)

在此页面中,我添加了要在活动中显示的Viewpager。我认为arraylist有问题,但是看起来很正常,因为我找不到任何问题。感谢您的帮助。

package com.example.proje;
 imports..

public class DetayActivity extends AppCompatActivity {

    ViewPager viewPager;
    LinearLayout sliderDotspanel;
    private int dotscount;
    private ImageView[] dots;
    private ArrayList<String> urls;
    String key;
    TextView txt1;
    TextView txt2;
    TextView txt3;
    TextView txt4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detay);
        setTitle("Detaylar");

        txt1 = findViewById(R.id.txttarih);
        txt2 = findViewById(R.id.txturunadi);
        txt3 = findViewById(R.id.txtfiyati);
        txt4 = findViewById(R.id.txtaciklama);

        Intent intent = getIntent();
        key = intent.getStringExtra("key");
        urls = new ArrayList<>();

        getdata();


        viewPager = findViewById(R.id.viewPager);

        sliderDotspanel = findViewById(R.id.SliderDots);


        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this , urls);

        viewPager.setAdapter(viewPagerAdapter);

        dotscount = viewPagerAdapter.getCount();
        dots = new ImageView[dotscount];

        for (int i = 0; i < dotscount; i++) {

            dots[i] = new ImageView(this);
            dots[i].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.non_active_dot));

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            params.setMargins(8, 0, 8, 0);

            sliderDotspanel.addView(dots[i], params);
        }

        dots[0].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.active_dot));

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < dotscount; i++) {
                    dots[i].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.non_active_dot));
                }
                dots[position].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.active_dot));
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

    private void getdata() {

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("urunler").child(key);

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                Urun urun = dataSnapshot.getValue(Urun.class);
                txt1.setText(urun.getTarih());
                txt2.setText(urun.getUrunAdi());
                txt3.setText(Double.toString(urun.getFiyat()));
                txt4.setText(urun.getAciklama());
                urls.add(urun.getResim1());
                urls.add(urun.getResim2());
                urls.add(urun.getResim3());

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
}
}

此页面是viewpager适配器

package com.example.proje;
imports..

public class ViewPagerAdapter extends PagerAdapter {

    private Context context;
    private LayoutInflater layoutInflater;
    private Integer [] images = {R.drawable.logo,R.drawable.doluredkalp,R.drawable.redheart};
    private ArrayList<String> urls = new ArrayList<>();



    public ViewPagerAdapter(Context context , ArrayList<String> urls) {
        this.context = context;
        this.urls = urls;

    }

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

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

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

        final String url = urls.get(position);

        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.layoutpager, null);
        ImageView imageView = view.findViewById(R.id.imageView);    
        Picasso.get().load(url).placeholder(R.drawable.logo).into(imageView);

        ViewPager vp = (ViewPager) container;
        vp.addView(view, 0);
        return view;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        ViewPager vp = (ViewPager) container;
        View view = (View) object;
        vp.removeView(view);

    }
}