Android: Ints.tryParse returns always "null", even if there's a number in the String

StackOverflow https://stackoverflow.com/questions/13661289

  •  04-12-2021
  •  | 
  •  

Вопрос

I've googled my problem and also searched here on stackoverflow for a solution, but I couldn't find a solution to my problem. I'm trying to implement a function which checks if the ANDROID_ID is already saved in the Apps database or if it's not. If the Device's ANDROID_ID is null, it creates a random UUID which is then saved in the DB. My problem is that everytime i try to parse the number from the String with "Ints.tryParse" it returns null, and then the app crashes. I've tried the app on two Galaxy S2's. It crashes on one of them, but works on the other one (one with Android 4.1.2 CyanogenMod, and one Stock 4.0). It also crashes in the Emulator. BTw, I'm using the Google Guava Library for "Ints.tryParse".

Here's the code:

    UUID uuid;
    int finalresult = 0;
    String android_id = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
    Log.v(TAG, "Android ID: " + android_id);
    String androidid = CharMatcher.DIGIT.retainFrom(android_id);
    Log.v(TAG, "Int0: " + androidid);
    Integer on2 = Ints.tryParse(androidid);
    if(on2 != null) {
        if(DEBUG_STATUS == 1) {
            Log.v(TAG, "Integer is: " + Integer.toString(on2));
        }
        int times = 3;
        finalresult = on2*times;
        if(DEBUG_STATUS == 1) {
            Log.v(TAG, "Result: " + Integer.toString(finalresult));
            Log.v(TAG, "Device ID: " + android_id + Integer.toString(finalresult));
        }
    }
    else
    {   
        uuid = UUID.randomUUID();
        String uuuid = CharMatcher.DIGIT.retainFrom(uuid.toString());
        Integer randomuuid = Ints.tryParse(uuuid);

        if(randomuuid == null) {
            Log.v(TAG, "Was null again...");
        }
        if(DEBUG_STATUS == 1) {
            Log.v(TAG, "Integer null!");
            Log.v(TAG, uuuid);
            Log.v(TAG, "UUID: " + randomuuid.toString());
        }
        int times = 3;
        finalresult = randomuuid.intValue()*times;
    }

Here's the LogCat from the emulator (Android 2.3.3):

> 12-01 16:14:22.505: V/******(410): Android ID: ea58466ebe002707 

> 12-01 16:14:51.845:V/******(410): Int0: 58466002707

> 12-01 16:14:51.865: V/******(410): Was null again...

> 12-01 16:14:51.875: V/******(410): Integer null!

> 12-01 16:14:51.875: V/******(410): 801094028350597

> 12-01 16:14:51.925: E/AndroidRuntime(410): FATAL EXCEPTION: main
> 12-01 16:14:51.925: E/AndroidRuntime(410): java.lang.RuntimeException:Unable to start activity ComponentInfo{*****/******}:
> .lang.NullPointerException 12-01 16:14:51.925:
> E/AndroidRuntime(410):    at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
> 12-01 16:14:51.925: E/AndroidRuntime(410):    at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
> 12-01 16:14:51.925: E/AndroidRuntime(410):    at
> android.app.ActivityThread.access$1500(ActivityThread.java:117) 12-01
> 16:14:51.925: E/AndroidRuntime(410):  at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
> 12-01 16:14:51.925: E/AndroidRuntime(410):    at
> android.os.Handler.dispatchMessage(Handler.java:99) 12-01
> 16:14:51.925: E/AndroidRuntime(410):  at
> android.os.Looper.loop(Looper.java:123) 12-01 16:14:51.925:
> E/AndroidRuntime(410):    at
> android.app.ActivityThread.main(ActivityThread.java:3683) 12-01
>  16:14:51.925: E/AndroidRuntime(410):     at
> java.lang.reflect.Method.invokeNative(Native Method) 12-01
> 16:14:51.925: E/AndroidRuntime(410):  at
> java.lang.reflect.Method.invoke(Method.java:507) 12-01 16:14:51.925:
> E/AndroidRuntime(410):    at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
> 12-01 16:14:51.925: E/AndroidRuntime(410):    at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 12-01
> 16:14:51.925: E/AndroidRuntime(410):  at
> dalvik.system.NativeStart.main(Native Method) 12-01 16:14:51.925:
> E/AndroidRuntime(410): Caused by: java.lang.NullPointerException 12-01
> 16:14:51.925: E/AndroidRuntime(410):  at
> com.actionbarsherlock.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:526)
> 12-01 16:14:51.925: E/AndroidRuntime(410):    at
> com.actionbarsherlock.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:452)
> 12-01 16:14:51.925: E/AndroidRuntime(410):    at
> com.actionbarsherlock.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:438)
> 12-01 16:14:51.925: E/AndroidRuntime(410):    at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
> 12-01 16:14:51.925: E/AndroidRuntime(410):    at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
> 12-01 16:14:51.925: E/AndroidRuntime(410):    ... 11 more

I honestly don't know what could be wrong... As you see, the String is not empty and it actually shows the ANDROID_ID from the device.

If there's more information you need, please tell me what you need, and I will edit the post then.

Это было полезно?

Решение

Use Long (Long.parseLong()) instead of Integer, because Integer.MAX_VALUE = 2 147 483 647, but your android_id is 58 466 002 707, and UUID is 801 094 028 350 597. Long.MAX_VALUE is 9 223 372 036 854 775 807.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top