You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中java.lang.ArrayIndexOutOfBoundsException问题求助

Fixing the ArrayIndexOutOfBoundsException in Your GridDetailActivity

Let's break down your problem and fix it step by step:

What's Causing the Crash?

Your error log clearly states:

java.lang.ArrayIndexOutOfBoundsException: length=8; index=14

This means you're trying to access the 14th index (remember, arrays are 0-indexed, so max valid index here is 7) of an array that only has 8 elements. Looking at your code:

  1. The position value comes from the Intent's "id" extra, which is 14 in this case.
  2. You're using this position to access arrays in MyGridAdapter (mThumbIds, mDescriptionTXT, Ingredients, mRecipeTitle) which only have 8 elements each.

The root issue is either:

  • You added new elements to your list but didn't update all the corresponding arrays in MyGridAdapter to match the new length.
  • The position value passed via Intent is incorrect (it's larger than the actual number of items in your adapter).

Step-by-Step Solutions

1. Sync All Adapter Arrays to Match Your New List

First, check your MyGridAdapter class. Make sure every array (mThumbIds, mDescriptionTXT, Ingredients, mRecipeTitle) has the same length and includes all the new elements you added. For example, if you added 7 new recipes, each array should now have 15 elements (8 original +7 new).

2. Add Boundary Checks to Prevent Invalid Access

Even if you fix the array lengths, it's good practice to add a safety check to handle invalid position values. Update your onCreate method:

Intent i = getIntent();
position = i.getExtras().getInt("id");

MyGridAdapter gridAdapter = new MyGridAdapter(this);

// Add this boundary check before accessing any arrays
if (position < 0 || position >= gridAdapter.mThumbIds.length) {
    Toast.makeText(this, "Invalid recipe selected", Toast.LENGTH_SHORT).show();
    finish(); // Close the activity if position is invalid
    return;
}

3. Verify Intent Position Values Are Correct

Double-check the code where you launch GridDetailActivity. Ensure you're passing a valid position that falls between 0 and gridAdapter.getCount() - 1. For example, if using a RecyclerView:

// In your RecyclerView click listener
Intent intent = new Intent(this, GridDetailActivity.class);
intent.putExtra("id", adapterPosition); // adapterPosition is the valid index from your list
startActivity(intent);

Modified onCreate Code Example

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

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(view -> Snackbar.make(view, "jojo", Snackbar.LENGTH_LONG)
            .setAction("Action", null).show());

    setTitle(" Grid Details Activity ");

    Intent i = getIntent();
    position = i.getExtras().getInt("id");

    MyGridAdapter gridAdapter = new MyGridAdapter(this);

    // Critical boundary check
    if (position < 0 || position >= gridAdapter.mThumbIds.length) {
        Toast.makeText(this, "Invalid recipe selected", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }

    // Note: The loop below creates ImageViews but doesn't use them—you can remove it if unused
    List<ImageView> mItems = new ArrayList<>();
    for (int pos = 0; pos < gridAdapter.getCount(); pos++) {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(gridAdapter.mThumbIds[pos]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        mItems.add(imageView);
    }

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

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

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

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

内容的提问来源于stack exchange,提问作者Waqas Ahmed Mir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:57:17