【问题标题】:android - java.lang.ArrayIndexOutOfBoundsException: length=4; index=4 length array - Stack Overflow [duplicate]android - java.lang.ArrayIndexOutOfBoundsException:长度=4; index = 4长度数组 - Thinbug
【发布时间】:2018-05-26 08:01:26
【问题描述】:

我在 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 在这里花费超过 5 毫秒。总经过时间(我们):5229 I/Process:发送信号。 PID:21865 SIG:9 与目标VM断开连接,地址:'localhost:8600',传输:'socket'


这是代码:

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]);


    }
}

我不知道如何处理数组长度问题。

我刚刚将新元素添加到列表中,但我不知道这是否是正确的做法。

我需要调整数组的大小吗?

【问题讨论】:

  • 您从Intent 获得的position 是14。您正在使用它来访问MyGridAdapter 中只有8 个元素的数组之一中的元素,其中最大索引为 7。我们不知道这些数组是什么,它们应该有多少元素,或者你甚至想要做什么。我们可以告诉您的就是不要尝试访问超出最大索引的数组中的元素。
  • 在你的 for 循环之后,你使用的 position 大于你的数组的大小。

标签: java android arrays indexoutofboundsexception lang


【解决方案1】:

第 71 行到底是什么?那会有所帮助。 我猜是gridAdapter.mThumbIds[position] 而你的问题不是mItems,这样很好,你正确地使用了一个列表。

但是 mThumbIds 是如何定义的或者它的目的是什么?我假设您无法更改它,因此请弄清楚如何将您的位置映射到 thumbsId。

例如position % 5 产生的值介于 0 和 4 之间,因此如果位置为 5,则为 0,如果位置为 6,则为 1,这样就循环了,但也许你需要另一个逻辑?

【讨论】:

    猜你喜欢
    • 2014-05-13
    • 2015-05-22
    • 1970-01-01
    • 2013-06-07
    • 2012-06-25
    • 2018-06-02
    • 1970-01-01
    • 2019-12-29
    • 2018-11-05
    相关资源
    最近更新 更多