Android中java.lang.ArrayIndexOutOfBoundsException问题求助
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:
- The
positionvalue comes from the Intent's"id"extra, which is 14 in this case. - You're using this
positionto access arrays inMyGridAdapter(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
MyGridAdapterto match the new length. - The
positionvalue 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

