Android:随机从数组中挑选字符串

时间:2013-07-13 15:42:09

标签: android

我正在尝试从数组中随机选择一个字符串并将其吐出给用户,但每当我尝试通过AVD运行它时,应用程序崩溃。我在这方面非常新手,无法弄清楚该怎么做。

这是我的代码:

public class MainActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final LinearLayout rr = new LinearLayout (this);

    // setting up the LinearLayout. I'll get to proper formatting one I get the code running.
    Button b1 = new Button (this);
    b1.setId(R.id.Button01);
    b1.setText("Generate");
    rr.addView(b1);
    final TextView tv;
    tv = new TextView(this);
    tv.setId(R.id.Text1);
    rr.addView(tv);
    setContentView(rr);

    final String [] columnA = { "artless", "bawdy", "beslubbering", "bootless", "churlish", "cockered", "clouted", "craven", "currish", "dankish", "dissembling", "droning", "errant", "fawning", "fobbing", "froward", "frothy", "gleeking", "goatish", "gorbellied", "impertinent", "infectious", "jarring", "loggerheaded", "lumpish", "mammering", "mangled", "mewling", "paunchy", "pribbling", "puking", "puny", "qualling", "rank", "reeky", "roguish", "ruttish", "saucy", "spleeny", "spongy", "surly", "tottering", "unmuzzled", "vain", "venomed", "villainous", "warped", "wayward", "weedy", "yeasty" };
    final String [] columnB = { "base-court", "bat-fowling", "beef-witted", "beetle-headed", "boil-brained", "clapper-clawed", "clay-brained", "common-kissing", "crook-patted", "dismal-dreaming", "dizzy-eyed", "doghearted", "dread-boiled", "earth-vexing", "elf-skinned", "fat-kidneyed", "fen-sucked", "flap-mouthed", "fly-bitten", "folly-fallen", "fool-born", "full-gorged", "guts-gripping", "hasty-witted", "half-faced", "hell-hated", "idle-headed", "ill-breeding", "ill-nurtured", "knotty-pated", "milk-livered", "motley-minded", "onion-eyed", "plume-plucked", "pottle-deep", "pox-marked", "reeling-riped", "rough-hewn", "rude-growing", "rump-fed", "shard-borne", "sheep-biting", "spur-galled", "swag-bellied", "tardy-gaited", "tickle-brained", "toad-spotted", "unchin-snouted", "weather-bitten"};
    final String [] columnC = { "apple-john", "baggage", "barnacle", "bladder", "boar-pig", "bugbear", "bum-bailey", "canker-blossom", "clack-dish", "clotpole", "coxcomb", "codpiece", "death-token", "dewberry", "flap-dragon", "flax-wench", "flirt-gill", "foot-licker", "fustilarian", "giglet", "gudgeon", "haggard", "harpy", "hedge-pig", "horn-beast", "hugger-mugger", "joithead", "lewdster", "lout", "maggot-pie", "malt-worm", "mammet", "measle", "minnow", "miscreant", "moldwarp", "mumble-news", "nut-hook", "pigeon-egg", "pignut", "puttock", "pumpion", "ratsbane", "scut", "skainsmate", "strumpet", "varlot", "vassal", "whey-face", "wagtail"};

    //Attempts to pick one string from each array, add "thou art a" and spaces, and display it to the device.
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String outputA;
            Random r;
            r = new Random(columnA.length);
            Random s;
            s = new Random(columnB.length);
            Random t;
            t = new Random(columnC.length);
            outputA = "Thou art a" + " " + columnA[r.nextInt() % columnA.length] + " " + columnB[s.nextInt() % columnB.length] + " " + columnC[t.nextInt() % columnC.length];
            tv.setText(outputA);
        }
    });
}


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

1 个答案:

答案 0 :(得分:2)

我想你得到的是ArrayIndexOutOfBoundsException

要解决此问题,请移除所有Random并将onClickListener更改为:

b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String outputA = "Thou art a" + " " + columnA[(int) (Math.random() * columnA.length)] + " " + columnB[(int) (Math.random() * columnB.length)] + " " + columnC[(int) (Math.random() * columnC.length)];
        tv.setText(outputA);
    }
});