App崩溃 - 随机图像生成器

时间:2018-02-05 18:44:28

标签: java android indexoutofboundsexception

我目前正在创建一个获取数组的程序,将其设置为等于图像列表,然后使用随机数生成器显示所有这些图像。我在我的物理三星Galaxy S7 Edge上运行应用程序。

代码:

package com.binnieapplications.secondapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

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

    Button doStuffButton;
    doStuffButton = (Button) findViewById(R.id.doStuffButton);

    final ImageView topAndroidImage = (ImageView) findViewById(R.id.topAndroidImage);

    final ImageView bottomAndroidImage = (ImageView) findViewById(R.id.bottomAndroidImage);

   final int[] pictureArray = {
            R.drawable.dragonite,
            R.drawable.garchomp,
            R.drawable.gardevoir};



    doStuffButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("RandomApp","Hello!");

            Random randomNumberGenerator = new Random();
            int number = randomNumberGenerator.nextInt(6);
            Log.d("RandomApp", "The random number is: " + number);

            bottomAndroidImage.setImageResource(pictureArray[number]);
        }
    });


}
   }

然后我收到此错误,应用程序崩溃了:

02-05 18:36:03.329 22413-22413/com.binnieapplications.secondapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.binnieapplications.secondapp, PID: 22413
                                                                                  java.lang.ArrayIndexOutOfBoundsException: length=3; index=5
                                                                                      at com.binnieapplications.secondapp.MainActivity$1.onClick(MainActivity.java:42)
                                                                                      at android.view.View.performClick(View.java:6213)
                                                                                      at android.widget.TextView.performClick(TextView.java:11074)
                                                                                      at android.view.View$PerformClick.run(View.java:23645)
                                                                                      at android.os.Handler.handleCallback(Handler.java:751)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

1 个答案:

答案 0 :(得分:0)

您应该将随机生成器从6更改为3,因为您的图片数组大小为3

int number = randomNumberGenerator.nextInt(3);

nextInt(3)方法调用返回伪随机,在0(包括)和3(不包括)之间均匀分布的int值。