type missmatch:无法从string转换为int

时间:2015-04-23 18:32:49

标签: android

public void clickresult(View v){
    String temp = ""; 
    int cards[] = {etnumber1.getText().toString(),etnumber2.getText().toString(),
                    etnumber3.getText().toString(),etnumber4.getText().toString()
                    ,etnumber4.getText().toString()};
    int noOfCard = cards.length;
        for (int marker = 1; marker < noOfCard; marker++) {
            for (int j = 0; j < marker; j++) {
                if (cards[marker] < cards[j]) {              
                    int tmp = cards[marker];
                    cards[marker] = cards[j];
                    cards[j] = tmp;
                }
            }
        }    
        for (int i = 0; i < noOfCard; i++) {
            temp += cards[i] + " ";
        }
        Toast.makeText(this, temp, Toast.LENGTH_LONG).show();
    }

它仍有一个错误 - &gt; type missmatch:无法从字符串转换为int

如何修复

int cards[] = {etnumber1.getText().toString(),etnumber2.getText().toString(),
                        etnumber3.getText().toString(),etnumber4.getText().toString()
                        ,etnumber4.getText().toString()};

2 个答案:

答案 0 :(得分:0)

public class MainActivity extends Activity {
EditText number1,number2,number3,number4,number5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    number1 = (EditText)findViewById(R.id.etnumber1);
    number2 = (EditText)findViewById(R.id.etnumber2);
    number3 = (EditText)findViewById(R.id.etnumber3);
    number4 = (EditText)findViewById(R.id.etnumber4);
    number5 = (EditText)findViewById(R.id.etnumber5);


}

public void clickresult(){
     int cards[] = {6,7,3,2,5};
     int noOfCard = cards.length;
        for (int marker = 1; marker < noOfCard; marker++) {
            for (int j = 0; j < marker; j++) {
                if (cards[marker] < cards[j]) {              
                    int tmp = cards[marker];
                    cards[marker] = cards[j];
                    cards[j] = tmp;
                }
            }
        }    
        for (int i = 0; i < noOfCard; i++) {
            System.out.print(cards[i] + " ");
        }
        System.out.println();
    }

答案 1 :(得分:0)

@Override
protected void onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.your_fragment_layout, container, false);

    etnumber1 = (EditText)findViewById(R.id.etnumber1);
    etnumber2 = (EditText)findViewById(R.id.etnumber2);
    etnumber3 = (EditText)findViewById(R.id.etnumber3);
    etnumber4 = (EditText)findViewById(R.id.etnumber4);
    etnumber5 = (EditText)findViewById(R.id.etnumber5);
}

public void onClickResult(){
    String cards[] = {etnumber1.getText().toString,
                        etnumber2.getText().toString(),
                        etnumber3.getText().toString(),
                        etnumber4.getText().toString(),
                        etnumber5.getText().toString()
    };

    int noOfCard = cards.length;
    Arrays.sort(cards);

    StringBuilder builder = new StringBuilder();
    for (String s : cards)
        builder.append(s).append(" ");

    Toast.makeText(context, builder.toString(), Toast.LENGTH_SHORT).show();
}

这将对您的阵列进行排序,并以Toast格式显示:“2 3 5 6 7”(根据您提供的数字)。如果EditText只包含数字,请考虑改为使用NumberPicker

LENGTH_SHORT也可以是LENGTH_LONG