I’m trying to get a “long” value from a String containing a Facebook ID, which is a 10 digit number. Here’s what my function looks like:
@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());
}
}
Normally, I would set long player_id to equal the conversion of playerUidString to long, but Long.getLong(playerUidString) results in null, while playUidString equals the 10 digit ID number. Any reason why this is happening?
Edit: My Log.d outputs:
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
You are missing the fact that
Long.getLong(String str)is not supposed to parse a String to a long, but rather to return a long value of a system property represented by that string.As others have suggested, what you actually need is
Long.parseLong(String str)Check out Java Document Here for more information.