2013-07-13 64 views
0

我想從數組中隨機挑選一個字符串,並將其吐出給用戶,但每當我嘗試通過AVD運行應用程序時崩潰。我在這方面是一個新手,並且不知道該怎麼做。Android:從數組中隨機挑選字符串

這裏是我的代碼:

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

我強烈建議你閱讀'Random'構造函數和'nextInt'方法的文檔。他們不會做你認爲他們做的事。 –

+0

由於@JonSkeet說檢查文檔。傳遞給Random構造函數的參數是隨機數生成器的種子。你可以把這個參數保留出來,因爲Random會使用一個「足夠好」的種子來處理你正在做的事情。您在nextInt調用中傳遞隨機數的範圍。 – HeatfanJohn

+0

當您詢問崩潰時,您還應該發佈logcat堆棧跟蹤。它確切地確定了問題所在。 – Simon

回答

2

我想你得到一個ArrayIndexOutOfBoundsException

要解決,刪除所有Random S和改變你的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); 
    } 
}); 
+0

我謝謝你!有用! – user2540201

+0

雖然使用'Math.random()'工作,但它不像使用「隨機」那樣乾淨(IMO) - 但正確地做到了這一點。 –

+0

@JonSkeet Fair dos。有時候所有的人都想要及時的解決方案:) –