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

682 views Asked by At

My Log Cat error is this but I am not getting this handler and looper error and also I have tried various things. This program is for sliding image automatically using view pager:

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

My MainActivity.class is

@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);
            }
        });

    }
}`

Also, there is error of Array Out Of BoundException in line 38 -

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

But I am not getting it how is there Array Out of Bound Exception. Kindly Help.

Thank you very much for your time and assistance in this matter.

1

There are 1 answers

1
ישו אוהב אותך On BEST ANSWER

The problem is probably from this code:

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

You need to check if your ViewPagerAdapter class is working or not. Then check if viewPagerAdapter.getCount(); is returning the correct size for the adapter items, because it is usually return 0 if you use auto generated code feature in your IDE. Something like this:

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

So, you need to return the size of your items there.