密钥Age,期望字符串的无效类型,但得到了数字

时间:2014-08-30 13:22:16

标签: java android android-layout android-activity

我正在尝试从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" />

1 个答案:

答案 0 :(得分:1)

嗯,非常简单

在这里,我正在给予相同的步骤

1)在xml中将其设置为编辑文本

android:inputType="number"

//以便edittext只接受数字

2)String text = yourET.getText().toString()

将文字作为字符串

3)将其解析为int

 int val =  Integer.parseInt(text);

欢呼声!!