我試圖從EditText年齡(用戶輸入他們的年齡)檢索一個數字,並將其存儲在解析中。這樣做,我收到以下錯誤:「無效類型的關鍵年齡,預期的字符串,但有號碼」關鍵字的無效類型Age,期望的字符串,但得到的數字
我想記錄「年齡」作爲一個數字,而不是一個字符串,但遇到困難。
下面是活動的代碼:
public class ProfileCreation extends Activity {
private static final int RESULT_LOAD_IMAGE = 1;
FrameLayout layout;
Button save;
protected EditText mName;
protected EditText mAge;
protected EditText mHeadline;
protected ImageView mprofilePicture;
RadioButton male, female;
String gender;
RadioButton lmale, lfemale;
String lgender;
protected SeekBar seekBarMinimum;
protected SeekBar seekBarMaximum;
protected SeekBar seekBarDistance;
protected Number age;
protected Button mConfirm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_creation);
RelativeLayout v = (RelativeLayout) findViewById(R.id.main);
v.requestFocus();
Parse.initialize(this, "ID", "ID");
mName = (EditText)findViewById(R.id.etxtname);
mAge = (EditText)findViewById(R.id.etxtage);
mHeadline = (EditText)findViewById(R.id.etxtheadline);
mprofilePicture = (ImageView)findViewById(R.id.profilePicturePreview);
male = (RadioButton)findViewById(R.id.rimale);
female = (RadioButton)findViewById(R.id.rifemale);
lmale = (RadioButton)findViewById(R.id.rlmale);
lfemale = (RadioButton)findViewById(R.id.rlfemale);
seekBarMinimum = (SeekBar) findViewById(R.id.sbseekBarMinimumAge);
seekBarMaximum = (SeekBar) findViewById(R.id.sbseekBarMaximumAge);
seekBarDistance = (SeekBar) findViewById(R.id.sbseekBarDistance);
mConfirm = (Button)findViewById(R.id.btnConfirm);
mConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mName.getText().toString();
// Number age = mAge.getText(;
String headline = mHeadline.getText().toString();
// age = ((String) age).trim();
name = name.trim();
headline = headline.trim();
if (name.isEmpty() || headline.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
builder.setMessage(R.string.signup_error_message)
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else {
// create the new user!
setProgressBarIndeterminateVisibility(true);
ParseUser currentUser = ParseUser.getCurrentUser();
if(male.isChecked())
gender = "Male";
else
gender = "Female";
if(lmale.isChecked())
lgender = "Male";
else
lgender = "Female";
age = Integer.parseInt(mAge.getText().toString());
currentUser.put("Name", name);
currentUser.put("Age", age);
特別地,我已經使用:
age = Integer.parseInt(mAge.getText().toString());
下面是指年齡
<EditText
android:id="@+id/etxtage"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/btnPictureSelect"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="Please type your age here"
android:inputType="number"
android:maxLength="2"
android:textAlignment="center"
android:textColor="#f2f2f2"
android:textSize="18dp" />
看來,你檢索你的int就好了。我想你會在最後一行得到你的錯誤:'currentUser.put(「Age」,age);'。那是對的嗎? – Tim 2014-08-30 13:28:55
你是正確的先生 – John 2014-08-30 13:34:42
儘管我不太清楚我將如何解決它,因爲我已經設法在使用諸如currentUser.put(「Minimum_Age」,seekBarMinimum.getProgress())之類的seekvalues之前解析數字,但它與EditText我遇到問題 – John 2014-08-30 13:53:06