android - java.lang.ArrayIndexOutOfBoundsException:length = 4; index = 4长度数组 - Stack Overflow

时间:2018-05-26 08:01:27

标签: java android arrays indexoutofboundsexception lang

我在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在这里需要5ms以上。总经过时间(美元):5229 I /处理:发送信号。 PID:21865 SIG:9 断开与目标VM的连接,地址:'localhost:8600',transport:'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]);


    }
}

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

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

我需要调整阵列的大小吗?

1 个答案:

答案 0 :(得分:0)

71号线究竟是什么?那会有所帮助。 我想这是gridAdapter.mThumbIds[position]而你的问题不是mItems,这样就好了,你正确使用了一个列表。

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

E.g。 position % 5产生的值介于0和4之间,所以如果position是5,那么它将是0,如果position是6,它将是1,所以这是循环,但也许你需要另一个逻辑?