简体   繁体   中英

Randomly pick an index from an array, display in TextView

I'm new to Android development and I'm wondering why my code crashes the Android Emulator. What I'm doing is creating an array of strings, then picking an index from the array at random and displaying the value inside a TextView . But it always seems to crash my emu.

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

        }
    };
}

Any help/advice would be great!

Thanks.

You created a String[] of size 7.

hellos = new String[7];

Therefore the indices range from 0 to 6. Trying to access hellos[7] will cause an IndexOutOfBoundsException .

I assume your getting a nullpointerexecption. Try generating your random number like this instead:

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

        }
    };
}

Increase string array size you gave 7 as size, but you are passing 8 values to string. so it throws indexoutofbounds exception.

这可能是因为数组IndexOutOfBoundException ...因为有时你的x将有值8但数组长度只有7 ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM