我試圖做一個小的應用程序,需要在一個EditText數量的輸入,並從一個數組採取相應的價值,並將其顯示在一個TextView設置TextView的數組值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText e = (EditText) findViewById(R.id.editText1);
final TextView t = (TextView) findViewById(R.id.textView1);
Button b = (Button) findViewById(R.id.button1);
Resources res = this.getResources();
String arr[] = getResources().getStringArray(R.array.example);
final Editable input = e.getText();
final String in2 = input.toString();
int number = Integer.parseInt(in2);
if(number < 0){
t.setText("Input is too small");
} else if (number > 666){
t.setText("Input is too large");
} else {
final String out = arr[number].toString();
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
t.setText(in2);
}
});
}
}
我想它這麼如果輸入爲0,按鈕被點擊TextView的是A.
<string-array name="example">
<item >A</item>
<item >B</item>
<item >C</item>
<item >D</item>
</string-array>
不幸的是,每當我開始這件事上的仿真器或它立即崩潰的電話。有誰知道我做錯了什麼?
stacktrace會很有幫助.. – simekadam