首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java.lang.ArrayIndexOutOfBoundsException: length=10;index=10

java.lang.ArrayIndexOutOfBoundsException: length=10;index=10
EN

Stack Overflow用户
提问于 2015-09-13 11:23:58
回答 1查看 7.8K关注 0票数 2

我已经开始开发有多个选择问题的应用程序,但当我尝试运行我的应用程序时,它说:

代码语言:javascript
复制
java.lang.ArrayIndexOutOfBoundsException: length=10; index=10

我知道上面写着什么,但我不明白问题出在哪里。

这是我的密码:

代码语言:javascript
复制
TextView tvq;
    InputStream is;
    String[] question = new String[10];
    ImageButton[] buttons = new ImageButton[4];
    int[] check = {-1, -1 ,-1};
    int[] answersid = {R.drawable.israelflag, R.drawable.spainflag, R.drawable.franceflag, R.drawable.greeceflag, R.drawable.egyptflag, R.drawable.unitedstatesflag, R.drawable.brazilflag, R.drawable.japanflag, R.drawable.turkeyflag, R.drawable.iraqflag};
    int i, randombutton, correctanswerid ,a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trivia);
        buttons[0] = (ImageButton) findViewById(R.id.im1);
        buttons[0].setOnClickListener(this);
        buttons[1] = (ImageButton) findViewById(R.id.im2);
        buttons[1].setOnClickListener(this);
        buttons[2] = (ImageButton) findViewById(R.id.im3);
        buttons[2].setOnClickListener(this);
        buttons[3] = (ImageButton) findViewById(R.id.im4);
        buttons[3].setOnClickListener(this);
        tvq = (TextView) findViewById(R.id.tvq);

        try {
            setQuestion();
        } catch (IOException e) {
            e.printStackTrace();
        }

        game();


    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_trivia, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void setQuestion() throws IOException {
        int z = 0;
        String st = "";
        is = this.getResources().openRawResource(R.raw.questions);
        InputStreamReader isr1 = new InputStreamReader(is);
        BufferedReader br1 = new BufferedReader(isr1);

        while ((st = br1.readLine()) != null) {
            question[z] = st;
            z++;
        }
        is.close();

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();



        if (id == R.id.im1) {
            if (R.id.im1 == correctanswerid)
                Toast.makeText(this, "correct", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(this, "incorrect", Toast.LENGTH_LONG).show();
            game();
        }
        if (id == R.id.im2) {
            if (R.id.im2 == correctanswerid)
                Toast.makeText(this, "correct", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(this, "incorrect", Toast.LENGTH_LONG).show();
            game();
        }
        if (id == R.id.im3) {
            if (R.id.im3 == correctanswerid)
                Toast.makeText(this, "correct", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(this, "incorrect", Toast.LENGTH_LONG).show();
            game();
        }
        if (id == R.id.im4) {
            if ( R.id.im4 == correctanswerid)
                Toast.makeText(this, "correct", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(this, "incorrect", Toast.LENGTH_LONG).show();
            game();
        }


    }

    public void game()
    {
        i = (int) (8 * Math.random());
        tvq.setText(question[i]);
        randombutton = (int) (3 * Math.random());
        buttons[randombutton].setImageResource(answersid[i]);
        correctanswerid = buttons[randombutton].getId();

        ImageButton temp = buttons[randombutton];
        buttons[randombutton] = buttons[buttons.length-1];



        for (int s = 0; s < buttons.length - 1; s++) {
            a = (int) (9 * Math.random());

                for (int k = 0; k < check.length ; k++) {
                    if (check[k] == a || a == i ) {
                        a = (int) (9 * Math.random());
                        while (check[k] == i || a == 1)
                            a = (int) (9 * Math.random());
                    }
                }

            check[s] = a;
            buttons[s].setImageResource(answersid[a]);
        }

    }

}

这是我的原始档案:

代码语言:javascript
复制
Israel?
Spain?
France?
Greece?
Egypt?
United States?
Brazil?
Japan?
Turkey?
Iraq?

谢谢你帮忙!(对不起,我的英语很差)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-13 11:47:29

问题在于问题数组。

代码语言:javascript
复制
String[] question = new String[10];

在这里,z的值超过9,这是问题数组的最大索引。

代码语言:javascript
复制
while ((st = br1.readLine()) != null) {
        question[z] = st;
        z++;
    }

你得阻止它超过9点。就像这样:

代码语言:javascript
复制
while (((st = br1.readLine()) != null) && (z < 10)) {
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32549137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档