我遇到了Array Out of Bound异常的运行时错误

时间:2017-09-16 05:49:42

标签: android runtime-error android-handler image-viewer

我的Log Cat错误是这个,但我没有得到这个处理程序和looper错误,我也尝试过各种各样的东西。此程序用于使用查看寻呼机自动滑动图像:

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.access$900(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.access$900(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));

但我没有得到它是如何有阵列超出限制的例外。请帮助。

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

1 个答案:

答案 0 :(得分:1)

问题可能源于此代码:

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

您需要检查ViewPagerAdapter班级是否有效。然后检查viewPagerAdapter.getCount();是否返回适配器项的正确大小,因为如果在IDE中使用自动生成的代码功能,则通常返回0 。像这样:

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

所以,你需要在那里返回你的物品的大小。