Android Question Strange crash while opening preferences

ErikM

Member
Licensed User
Longtime User
Hi, I am getting this strange crash:
B4X:
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Service (synchronize) Start **
** Activity (main) Pause, UserClosed = false **
java.lang.RuntimeException: Unable to start activity ComponentInfo{sk.trisoft.idoitlater/anywheresoftware.b4a.objects.preferenceactivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
    at android.app.ActivityThread.access$700(ActivityThread.java:134)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4867)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at anywheresoftware.b4a.objects.preferenceactivity$PreferenceScreenWrapper.createPreference(preferenceactivity.java:169)
    at anywheresoftware.b4a.objects.preferenceactivity.onCreate(preferenceactivity.java:41)
    at android.app.Activity.performCreate(Activity.java:5047)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
    ... 11 more
GC_CONCURRENT freed 376K, 17% free 9692K/11655K, paused 9ms+2ms, total 47ms
GC_CONCURRENT freed 384K, 17% free 9692K/11655K, paused 19ms+3ms, total 65ms

I am creating the preference screen here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        CreatePreferenceScreen
        If manager.GetAll.Size = 0 Then SetDefaults
        'get api key from settings
        key = manager.GetString("APIKey")
        keyValid = True
       
        If (key == "") Then
            StartActivity(screen.CreateIntent)
        End If
               
    End If
End Sub

Sub CreatePreferenceScreen
    screen.Initialize("I do it later", "")
    'create two categories
    Dim cat1 AsPreferenceCategory
    cat1.Initialize("Basic settings")
    cat1.AddEditText("APIKey", "API key", "Enter your API key as found on website", "")
    'add the categories to the main screen
    screen.AddPreferenceCategory(cat1)
End Sub

And calling it like this:
B4X:
StartActivity(screen.CreateIntent)

It does not happen always, only when I first stop application by back button and then click on the icon again and then try to call settings
 

ErikM

Member
Licensed User
Longtime User
Yes, in globals, I missed it in the tutorial. So I temporarily fixed it by moving the calling of CreatePreferenceScreen outside of "
If FirstTime Then" condition. It worked too ;)
But I felt that it is not the cleanest solution..
 
Upvote 0
Top