我正面临 运行 时间错误和数组越界异常

I am facing run time error with Array Out of Bound exception

我的 Log Cat 错误是这样的,但我没有收到此处理程序和循环程序错误,而且我尝试了各种方法。此程序用于使用视图寻呼机自动滑动图像:

09-16 10:53:25.326 10063-10063/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.admin.myapplication, PID: 10063
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.myapplication/com.example.admin.myapplication.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
                                                       at android.app.ActivityThread.access0(ActivityThread.java:153)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:148)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5438)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
                                                    Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                       at com.example.admin.myapplication.MainActivity.onCreate(MainActivity.java:38)
                                                       at android.app.Activity.performCreate(Activity.java:6303)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) 
                                                       at android.app.ActivityThread.access0(ActivityThread.java:153) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:148) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5438) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)  09-16 10:53:25.332 10063-10063/? I/Process: Sending signal. PID: 10063 SIG: 9

我的MainActivity.class是

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewPager = (ViewPager) findViewById(R.id.viewPager);
    slideDotspanel = (LinearLayout) findViewById(R.id.SliderDots);
    ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
    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.unactive_dot));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(8, 0, 8, 0);
        slideDotspanel.addView(dots[i], params);
    }

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

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new MyTimerTask(), 2000, 4000);

    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.unactive_dot
                                                  ));

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

                                          }


                                          @Override
                                          public void onPageScrollStateChanged(int state) {

                                          }
                                      }

    );

}

public class MyTimerTask extends TimerTask {
    public void run() {
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {


                if (viewPager.getCurrentItem() == 0)

                {
                    viewPager.setCurrentItem(1);

                } else if (viewPager.getCurrentItem() == 1)

                {
                    viewPager.setCurrentItem(2);
                } else
                    viewPager.setCurrentItem(0);
            }
        });

    }
}`

另外第38行有Array Out Of BoundException的错误-

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

但我不明白怎么会有数组越界异常。请帮助。

非常感谢您在这件事上的时间和帮助。

问题可能来自这段代码:

ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
viewPager.setAdapter(viewPagerAdapter);
dotscount = viewPagerAdapter.getCount();
dots = new ImageView[dotscount];

您需要检查您的 ViewPagerAdapter class 是否正常工作。然后检查 viewPagerAdapter.getCount(); 是否 return 适配器项目的正确大小,因为如果您使用自动生成的代码功能,它通常是 return 0在你的 IDE 中。像这样:

@Override
public int getCount() {
  return 0; // This is wrong!
}

因此,您需要 return 那里的项目大小。