我正在尝试接收用户输入作为代表我的彩票Android应用程序中的数字的字符串。然后我将此用户输入解析为整数并存储在数组中,以与另一个整数数组进行比较。但是我遇到以下问题:我遇到类型不匹配:无法从int转换为String。我创建了一个显示用户消息的活动,但我希望将其存储在数组中,这将是他们的彩票号码。我有一个链接到这个新的“显示数字活动”的按钮。我创建了一个全局变量“static String [] userNumbers = new String [SIZE]并将常量大小设置为= 6.
我在将String解析为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);
}
答案 0 :(得分:1)
在这一行
String userNumbers = editText.getText().toString();
定义String类型的局部变量userNumbers
。这个定义使得类字段userNumbers
不可访问。
您还尝试在String变量中存储int。这是一种类型不匹配。