0

私の宝くじAndroidアプリで数字を表す文字列としてユーザー入力を受信しようとしています。次に、このユーザー入力を解析して整数に変換し、配列に格納して、別の整数配列と比較します。ただし、型の不一致が発生しているという点で、次の問題があります。int から String に変換できません。ユーザー メッセージを表示するアクティビティを作成しましたが、これを配列に格納する必要があります。これが宝くじの番号になります。この新しい「数字の表示アクティビティ」にリンクするボタンがあります。グローバル変数 "static String [] userNumbers = new String[SIZE] を作成し、定数サイズを = 6 に設定しました。

文字列を int に解析するコードのセクションで不一致エラーが発生し、配列を設定するための for ループでエラーが発生します。誰かがこの問題を解決するのを手伝ってくれることを願っています。前もって感謝します!

私のコードは以下の通りです:

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = ".com.example.lotterychecker.MESSAGE";
    static boolean bonus = false;
    static boolean jackpot = false;
    static int lottCount = 0;
    final static int SIZE =6; 
    static String [] userNumbers = new String[SIZE]; 
    Button check;

//...some code for parsing html......

public void checkNumbers(View view) {
        //create an intent to display the numbers
        Intent intent = new Intent(this, DisplayNumbersActivity.class);
        EditText editText = (EditText) findViewById(R.id.enter_numbers);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message );
        startActivity(intent);

        String userNumbers = editText.getText().toString();
        userNumbers = Integer.parseInt(message); //mismatch error here
        Toast.makeText(MainActivity.this, "Here are your numbers", Toast.LENGTH_LONG).show();

        for (int count =0; count < SIZE; count ++)
        {
            if (check.isPressed())
            {
                userNumbers[count] = editText.getText().toString(); //error "The type of the expression must be an array type  
                                                                    //but it resolved to String" in the userNumbers[count] syntax
            }
        }//for
    }



public class DisplayNumbersActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_numbers);
        // Show the Up button in the action bar.
        setupActionBar();

        //get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        //create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        //set the text view as the activity layout
        setContentView(textView);
    }
4

1 に答える 1

1

この行で

String userNumbers = editText.getText().toString();

userNumbersString 型のローカル変数を定義します。この定義により、userNumbers配列であるクラス フィールドにアクセスできなくなります。

また、String 変数に int を格納しようとします。これは型の不一致です。

于 2013-08-24T15:46:33.340 に答える