我想打印A,B,C .....系列,但是当我点击按钮时,很遗憾就停止了,
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView alphabets = (TextView) findViewById(R.id.alphabet);
Button next = (Button) findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] alpha= {"A","B","C" ,"D" ,"E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
int alp = alpha.length;
for (int i=0;i<=alpha.length;i++)
alphabets.setText(alpha[i]);
}
};
next.setOnClickListener(listener);
答案 0 :(得分:0)
原因是IndexOutOfBoundException
:
for (int i=0;i<=alpha.length;i++)
alphabets.setText(alpha[i]);
}
将其更改为
for (int i=0;i<alpha.length;i++)
alphabets.setText(alpha[i]);
}
使用您的代码尝试输入最后一个元素+1并且该元素不存在。
顺便说一句。您的textview
只会显示最后一个元素!
编辑:
String[] alpha= {"A","B","C" ,"D" ,"E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
int alp = alpha.length;
StringBuffer alphabet = new StringBuffer();
for (int i=0;i<alpha.length;i++){
alphabet.append(alpha[i]);
}
alphabets.setText(alphabet.toString());
答案 1 :(得分:-1)
试试这个
String alpha[30];
alpha= {"A","B","C" ,"D" ,"E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};