我是新来的android编程,我尝试通过在一个EditText中输入数字来进行冒泡排序,排序的数字将在第二个EditText上输出。一旦我点击排序按钮,程序意外停止。请告诉我什么是错的。Android冒泡排序不起作用
package com.example.sorting;
import android.os.Bundle;
import android.app.Activity;
import android.text.Spannable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity{
EditText enterNum;
EditText sortedNum;
Button sortNow;
int num[] = new int[5];
int i, n, j;
int temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enterNum = (EditText)findViewById(R.id.enterNum);
sortedNum = (EditText)findViewById(R.id.sortedNum);
sortNow = (Button)findViewById(R.id.sortNow);
sortNow.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
BubbleSort();}});
}
public void BubbleSort(){
Spannable spn=enterNum.getText();
num[5]=Integer.valueOf(spn.toString());
for (i=0;i<5;i++){
for(j=i+1;j<5;j++){
if(num[i]>num[j]){
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
sortedNum.setText(String.valueOf(num[0])+String.valueOf(num[1])+String.valueOf(num[2])+String.valueOf(num[3])+String.valueOf(num[4]));
}
}
+0
logcat说什么? –
+0
“意外停止”是什么意思?例外?如果是,哪一行和哪一行?你需要更加精确,我们不会为你毫无痕迹地进行调试。 –
+0
这里的日志: 04-02 08:20:04.150:E/AndroidRuntime(1320):致命例外:main 04-02 08:20:04.150:E/AndroidRuntime(1320):java.lang.ArrayIndexOutOfBoundsException 04-02 08:20:04.150:E/AndroidRuntime(1320):\t at com.example.sorting.MainActivity.BubbleSort(MainActivity.java:39) 04-02 08:20:04.150:E/AndroidRuntime(1320) :\t at com.example.sorting.MainActivity $ 1.onClick(MainActivity.java:31) 04-02 08:20:04.150:E/AndroidRuntime(1320):\t at android.view.View.performClick(View.java :2485) 04-02 08:20:04.150:E/AndroidRuntime(1320):\t at android.view.View $ PerformClick.run(View.java:9081) –