簡體   English   中英

從數組中隨機選擇一個索引,在TextView中顯示

[英]Randomly pick an index from an array, display in TextView

我是Android開發的新手,我想知道為什么我的代碼崩潰Android模擬器。 我正在做的是創建一個字符串數組,然后隨機從數組中選擇一個索引並在TextView顯示該值。 但似乎總是讓我的鴯crash崩潰。

package com.test.randomTest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class randomTestActivity extends Activity {

    private Button button;
    private TextView helloTextView;
    private String[] hellos;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        helloTextView = (TextView)findViewById(R.id.helloText);
        button = (Button)findViewById(R.id.button);

        hellos = new String[7];
        hellos[0] = "Hello";
        hellos[1] = "G'days";    
        hellos[2] = "Yo!";
        hellos[3] = "Hi";
        hellos[4] = "Hay";
        hellos[5] = "Bonjour";
        hellos[6] = "Hay there!";
        hellos[7] = "Hallo";

        button.setOnClickListener(buttonListener);

    }

    private OnClickListener buttonListener = new OnClickListener() {

        public void onClick(View v) {

            int x = 0 + (int)(Math.random() * ((7 - 0) + 1));
            String helloText = hellos[x];
            helloTextView.setText(helloText);

        }
    };
}

任何幫助/建議都會很棒!

謝謝。

您創建了一個大小為7的String[]

hellos = new String[7];

因此索引范圍從0到6.嘗試訪問hellos[7]將導致IndexOutOfBoundsException

我假設你得到了一個nullpointerexecption。 嘗試生成這樣的隨機數:

Random rando = new Random();
int x = rando.nextInt(hellos.lenght);
package com.test.randomTest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class randomTestActivity extends Activity {

    private Button button;
    private TextView helloTextView;
    private String[] hellos;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        helloTextView = (TextView)findViewById(R.id.helloText);
        button = (Button)findViewById(R.id.button);

        hellos = new String[8];
        hellos[0] = "Hello";
        hellos[1] = "G'days";    
        hellos[2] = "Yo!";
        hellos[3] = "Hi";
        hellos[4] = "Hay";
        hellos[5] = "Bonjour";
        hellos[6] = "Hay there!";
        hellos[7] = "Hallo";

        button.setOnClickListener(buttonListener);

    }

    private OnClickListener buttonListener = new OnClickListener() {

        public void onClick(View v) {

            int x = 0 + (int)(Math.random() * ((7 - 0) + 1));
            String helloText = hellos[x];
            helloTextView.setText(helloText);

        }
    };
}

增加您給出7作為大小的字符串數組大小,但是您將8個值傳遞給字符串。 所以它拋出indexoutofbounds異常。

這可能是因為數組IndexOutOfBoundException ...因為有時你的x將有值8但數組長度只有7 ..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM