0

I got this error in android studio:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.bravo.developers.pak.indo.yaami.recipes.free, PID: 21865
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bravo.developers.pak.indo.yaami.recipes.free/com.bravo.developers.pak.indo.yaami.recipes.free.GridDetailActivity}: java.lang.ArrayIndexOutOfBoundsException: length=8; index=14
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)
                  at android.app.ActivityThread.access$800(ActivityThread.java:178)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
                  at android.os.Handler.dispatchMessage(Handler.java:111)
                  at android.os.Looper.loop(Looper.java:194)
                  at android.app.ActivityThread.main(ActivityThread.java:5653)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.ArrayIndexOutOfBoundsException: length=8; index=14
                  at com.bravo.developers.pak.indo.yaami.recipes.free.GridDetailActivity.onCreate(GridDetailActivity.java:71)
                  at android.app.Activity.performCreate(Activity.java:6100)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:178) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
                  at android.os.Handler.dispatchMessage(Handler.java:111) 
                  at android.os.Looper.loop(Looper.java:194) 
                  at android.app.ActivityThread.main(ActivityThread.java:5653) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

W/MALI: glDrawArrays:714: [MALI] glDrawArrays takes more than 5ms here. Total elapse time(us): 5229 I/Process: Sending signal. PID: 21865 SIG: 9 Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'


This is the code:

public class GridDetailActivity extends AppCompatActivity {

    int position;
    private ImageView imageView;
    private TextView textView;
    private TextView dtextView;
    private TextView titletextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_detail);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "jojo", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        // Set tile for the ViewPager
        setTitle(" Grid Details Activity ");

        // get intent data
        Intent i = getIntent();
        position = i.getExtras().getInt("id");

        MyGridAdapter gridAdapter = new MyGridAdapter(this);
        // List<MyGridAdapter.Item> mItems = new ArrayList<MyGridAdapter.Item>();

        List<ImageView> mItems = new ArrayList<ImageView>();

        // Retrieve all the images
        for (int position = 0; position < gridAdapter.getCount(); position++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(gridAdapter.mThumbIds[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            mItems.add(imageView);
        }

        imageView = (ImageView)findViewById(R.id.image_grddetails);
        imageView.setImageResource(gridAdapter.mThumbIds[position]);

        textView = (TextView)findViewById(R.id.description_TextView);
        textView.setText(gridAdapter.mDescriptionTXT[position]);

        dtextView = (TextView)findViewById(R.id.details_text);
        dtextView.setText(gridAdapter.Ingredients[position]);

        titletextView = (TextView)findViewById(R.id.restitle);
        titletextView.setText(gridAdapter.mRecipeTitle[position]);


    }
}

I don't know how to manage the array length problem.

I just added the new elements to the list, but I don't know if this is the right form to do it.

Do I need to resize my array?

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
  • The `position` you're getting from the `Intent` is 14. You're using it to access an element in one of the arrays in your `MyGridAdapter` that has only 8 elements, with a maximum index of 7. We have no idea what those arrays are, how many elements they should have, or what you're even trying to do. About all we can tell you is don't try to access elements in an array beyond the maximum index. – Mike M. May 26 '18 at 08:14
  • And also after your for loop you are using `position` which is larger than size of your array. – pouyan May 26 '18 at 08:15

1 Answers1

0

What is Line 71 exactly? That would help. I guess it's gridAdapter.mThumbIds[position] and your problem is not mItems, that's fine that way, you're correctly using a list.

However how is mThumbIds defined or what's it's purpose? I assume you can't change it, so figure out how to map your position to the thumbsId.

E.g. position % 5 yields values between 0 and 4, so if position is 5 it will be 0, if position is 6, it will be 1, so that is circulating, but maybe you need another logic?

MeFisto94
  • 157
  • 5