2014-08-30 53 views
0

我試圖從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

看來,你檢索你的int就好了。我想你會在最後一行得到你的錯誤:'currentUser.put(「Age」,age);'。那是對的嗎? – Tim 2014-08-30 13:28:55

+0

你是正確的先生 – John 2014-08-30 13:34:42

+0

儘管我不太清楚我將如何解決它,因爲我已經設法在使用諸如currentUser.put(「Minimum_Age」,seekBarMinimum.getProgress())之類的seekvalues之前解析數字,但它與EditText我遇到問題 – John 2014-08-30 13:53:06

回答

1

阱的佈局部分,其相當簡單

個在這裏我給步驟TI acheive相同

1)編輯文本設置這個在XML

android:inputType="number" 

//使EditText上僅接受數字

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

GET文本爲字符串

3)解析爲int

int val = Integer.parseInt(text); 

歡呼聲!

+0

感謝您的回覆。我已經完成了這些步驟,所以我覺得這個錯誤是從其他地方得到的,可能來自currentUser.put(「Age」,age);. – John 2014-08-30 13:39:07

+0

問題已解決。問題是我已經有一個Parse中的列被分類爲一個字符串,所以它試圖給字符串列添加一個數字值,所以我創建了一個新的列,並且案例得到了解決。 – John 2014-08-30 15:00:21

+0

歡迎伴侶:) – 2014-09-02 05:08:23

相關問題