2011-07-25 61 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 ..