2011-07-25 64 views
0

我是Android開發的新手,我想知道爲什麼我的代碼崩潰了Android模擬器。我正在做的是創建一個字符串數組,然後從數組中隨機選取一個索引並在TextView內顯示值。但它似乎總是讓我em crash不安。從數組中隨機選擇一個索引,在TextView中顯示

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

     } 
    }; 
} 

任何幫助/建議將是偉大的!

謝謝。

回答

2

你創造了大小7

hellos = new String[7]; 

因此指數範圍從0的String[]至6嘗試訪問hellos[7]將導致IndexOutOfBoundsException

+0

這是解決辦法,顯然是錯誤現在已經變得清晰起來。謝謝你。 – dotty

0

我假設你得到一個nullpointerexecption。嘗試產生這樣的而不是你的隨機數:

Random rando = new Random(); 
int x = rando.nextInt(hellos.lenght); 
0
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); 

     } 
    }; 
} 
0

增加字符串數組的大小你給了7作爲大小,但你傳遞8個值到字符串。 所以它會引發indexoutofbounds異常。

0

那可能是因爲陣列IndexOutOfBoundException的......因爲有時你的X將有值8,但數組的長度只有7 ..