NumberFormatException:无效的int - String

时间:2014-09-02 10:19:46

标签: java android android-activity android-fragments parse-platform

我在尝试从Parse上的数字列中检索数字时收到了以下异常 - NumberFormatException:Invalid int

我想检索数字,然后在TextView中显示数字。我已经能够用字符串实现这一点,但是数字有问题。

下面是logcat消息:

09-02 10:00:27.881: E/AndroidRuntime(1267): FATAL EXCEPTION: main
09-02 10:00:27.881: E/AndroidRuntime(1267): Process: com.dooba.beta, PID: 1267
09-02 10:00:27.881: E/AndroidRuntime(1267): java.lang.NumberFormatException: Invalid int: "UserAge"
09-02 10:00:27.881: E/AndroidRuntime(1267):     at java.lang.Integer.invalidInt(Integer.java:137)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at java.lang.Integer.parse(Integer.java:374)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at java.lang.Integer.parseInt(Integer.java:365)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at java.lang.Integer.parseInt(Integer.java:331)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at com.dooba.beta.Fragment1$6.done(Fragment1.java:371)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at com.parse.FindCallback.internalDone(FindCallback.java:45)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at com.parse.FindCallback.internalDone(FindCallback.java:31)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at com.parse.Parse$5$1.run(Parse.java:891)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at android.os.Handler.handleCallback(Handler.java:733)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at android.os.Looper.loop(Looper.java:136)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at java.lang.reflect.Method.invokeNative(Native Method)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at java.lang.reflect.Method.invoke(Method.java:515)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-02 10:00:27.881: E/AndroidRuntime(1267):     at dalvik.system.NativeStart.main(Native Method)

以下是代码

query5.findInBackground(new FindCallback<ParseUser>() {

                @Override
                public void done(List<ParseUser> objects,ParseException e) {

                    for(int i=0;i<objects.size();i++){
                        // Do whatever you need to extract object from "users"
                        ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
                        query.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
                                .getObjectId());
                        // users with Gender = currentUser.Looking_Gender
                        query.whereEqualTo("Gender", userLookingGender);
                        // users with Looking_Gender = currentUser.Gender
                        query.whereEqualTo("Looking_Gender", userGender);
                        query.setLimit(1);
                        query.whereEqualTo("ActivityName", activityName);
                        query.whereGreaterThanOrEqualTo("UserAge", minimumAge);
                        query.whereLessThanOrEqualTo("UserAge", maximumAge);

                        mUserAgeRetrieved = (TextView) getActivity().findViewById(R.id.userlistage);

                        String str = "UserAge";
                        int iage = Integer.parseInt(str);
                        mUserAgeRetrieved.setText(iage); 



                    }
        }//for loop
                });

仅供比较,以下是我检索字符串的方法:

mUserNameRetrieved.setText(objects.get(i).get("Name").toString()); 

“UserAge”指的是我正在调用以检索其中的数字的解析中的数字列。

提前致谢

2 个答案:

答案 0 :(得分:2)

这有什么意义?

String str = "UserAge";
int iage = Integer.parseInt(str);
mUserAgeRetrieved.setText(iage);

显然它会抛出NumberFormatException,因为“UserAge”不是数字!你不是在这里从ParseQuery中检索任何数字。

编辑: 使用mUserAgeRetrieved.setText(String.valueOf(objects.get(i).getInt(“UserAge”)))来检索数字。

答案 1 :(得分:1)

你必须改变这一行:

String str = "UserAge";

 String str = mUserAgeRetrieved.getText();