字符串到空值的长结果

时间:2012-06-29 17:36:06

标签: android

我正在尝试从包含Facebook ID的字符串中获取“”值,这是 10位数字。这是我的功能:

@Override
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
        try {
            final long opponentId;            
            opponentId = jsonArray.getJSONObject(position).getLong("uid");
            String opponentName = jsonArray.getJSONObject(position).getString("name");
            Toast.makeText(this, "UID: " + opponentId + "\n" + "Name: " + opponentName, Toast.LENGTH_SHORT).show();

            SharedPreferences myPrefs = getSharedPreferences("user", MODE_WORLD_READABLE);
            String playerUidString = myPrefs.getString("uid", null);


            if (playerUidString == null) {
                Log.d(GlobalVars.TAG, "Empty UID");
                return;
            }

            Log.d(GlobalVars.TAG, "ID: " + playerUidString);
            Log.d(GlobalVars.TAG, "ID Long: " + String.valueOf(Long.getLong(playerUidString)));

//           long player_id = Long.getLong(playerUidString);
//          new GameRequest(getBaseContext(), player_id, opponentId);


        } catch (JSONException e) {
            Log.d(GlobalVars.TAG, e.getMessage());
        }
    }

通常,我会设置long player_id等于playerUidString到long的转换,但是Long.getLong(playerUidString)会导致null,而playUidString则等于10位ID ID。出现这种情况的原因是什么?

编辑:我的Log.d输出:

06-29 11:06:37.823: D/I See It(16053): ID: 1089490706
06-29 11:06:37.823: D/I See It(16053): ID Long: null

2 个答案:

答案 0 :(得分:2)

您错过了Long.getLong(String str)不应将String解析为long的事实,而是返回由该字符串表示的系统属性的长值。 正如其他人所建议的那样,您实际需要的是Long.parseLong(String str)

Check out Java Document Here for more information

答案 1 :(得分:1)

你必须使用Long.parseLong(yourstring)

我希望我能帮助你。