Error with Preference Activity

Scantech

Well-Known Member
Licensed User
Longtime User
Hello

Using Preference Library version 1.01. Market upload shows this error. Any ideas? Thank you

java.lang.RuntimeException: Unable to start activity ComponentInfo{Scantech.CarGaugePro/anywheresoftware.b4a.objects.preferenceactivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at anywheresoftware.b4a.objects.preferenceactivity$PreferenceScreenWrapper.createPreference(preferenceactivity.java:167)
at anywheresoftware.b4a.objects.preferenceactivity.onCreate(preferenceactivity.java:41)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
 

Penko

Active Member
Licensed User
Longtime User
Sorry for hijacking the thread but I had exactly the same problem three weeks ago:

java.lang.RuntimeException: Unable to start activity ComponentInfo{PACKAGE_HERE/anywheresoftware.b4a.objects.preferenceactivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4385)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
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.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)

Here is my code:

B4X:
' this is called in Main
Sub prepareAppSettings()
   
   Common.CreatePreferenceScreen
      
      If Common.scrmanager.GetAll.Size = 0 Then 
         Common.SetDefaults
      End If
   
End Sub

B4X:
   Sub SetDefaults

   ' General
   scrManager.SetBoolean("tactileFeedback", True)
   
   ' Examing
   scrManager.SetBoolean("barcodeDirectly", True)
   scrManager.SetBoolean("barcodeScanerAutomatic", False)

End Sub

Sub CreatePreferenceScreen
   
   scrScreen.Initialize("Настройки", "")
   'create two categories
   
   Dim cat1, cat2 As PreferenceCategory
   cat1.Initialize("Общи")
   'cat1.AddCheckBox("displayConfirmations", "Show confirmation dialogs", "E.g Delete Operations will require a confirmation.", True)
   cat1.AddCheckBox("tactileFeedback", "Тактилна обратна връзка", "Телефонът вибрира когато работите с определени компоненти.", True)
   
   cat1.AddCheckBox("barcodeDirectly", "Баркод от камерата ==> проверка", "Не се изисква натискането на бутона Провери ако сте прочели баркода с камерата.", True)
   cat1.AddCheckBox("barcodeScanerAutomatic", "Отваряй скенера автоматично", "При проверка на продукт директно се отваря прозорецът за сканиране на Баркод.", False)
   
   'add the categories to the main screen
   scrScreen.AddPreferenceCategory(cat1)
   
End Sub

What I can say about it is that it happens rarely. Also, the platform is always "OTHER" when an error occurs. I thought it's a bug in the specific Android implementation on that phone and gave it up as it has happen only 7 times out of 10 000 installations. But it's good if you can check whether it's a bug in B4A or not.
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
Erel, now that I work on another application, I noticed that it crashes if the application is started but not focused for some time. Then, if the user returns to my app and goes to settings, the application crashes with force close.

For now, this happens only on Android 2.2, I am trying to crash it on my tablet with Android 4.0 too.

Unfortunately, I have to wait for other 15 minutes to crash it again on my Desire. If it crashes and then I go back to settings, the screen is loaded correctly.

The main difference with my previous application(see my previous post) is that now it happens much more frequently than before.

java.lang.RuntimeException: Unable to start activity ComponentInfo{##PACKAGE_HIDDEN##/anywheresoftware.b4a.objects.preferenceactivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
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.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
... 11 more
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
Then I might have two options.

Here is the snippet which calls the PreferenceActivity.

B4X:
Sub listSettingTypes_ItemClick (Position As Int, Value As Object)

   Select Value
      
      Case "general"
      
         StartActivity(zPreferenceActivity.screen.CreateIntent)
         
      Case "styles"
         StartActivity(aStylesList)
         
      Case "tts_example"
         zNarrator.speakTestMessage
         tmrTestTTS.Enabled = True
   End Select
   
End Sub

Since manager.isInitialized doesn't exist, should I re-work the "general" Case like this:

B4X:
Try
zPreferenceActivity.CreateScreen ' this function works, don't worry about it
Catch

End try

StartActivity(zPreferenceActivity.screen.CreateIntent)

I mean, should I ensure the initialization this way? Is it safe to initialize the screen if it is already initialized or that will produce an error?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Never tested the preferences lib, but watching Erel's example, what if you try calling the 'CreatePreferenceScreen' in activity_create, without the firstTime condition?
Now, in order to see whether screen is initialized, you can always use a process_global boolean flag. If this flag is destroyed, I guess same thing goes for the 'initialized' item.
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
Thats one solution but I cant be sure this is due to a killing of my application. I remember Erel saying process globals will be affected only in the event the OS kills your app.

For now i am just calling the create function every time and it seems to be working. It will indeed need further observation.

Thanks.

Sent from my Next7P12 using Tapatalk 2
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
Arh! I am getting reports in Google Play console for this error.

However, while developing today, I encountered it myself. I've seen multiple forum members to report similar errors. Note that if you go to Settings immediately after the crash, they open just fine.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kmsengineering.program/anywheresoftware.b4a.objects.preferenceactivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
at android.app.ActivityThread.access$1500(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
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.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
... 11 more

How is it possible that they work 100 times and fail on 101th?

This is how I initialize:(I removed cat2, cat3 additions to make the code easier to read)
B4X:
Sub CreatePreferenceScreen
    
   screen.Initialize("Settings", "")
    'create two categories
    Dim cat1, cat2, cat3 As PreferenceCategory
    cat1.Initialize("Digital Value Range")
    cat1.AddEditText("aaaaa", "Low Value", "", 0)
    cat1.AddEditText("bbbb", "High Value", "This is the High Value of the range", 1384)
   
   cat1.AddList("tccccc", "Signal type", "The type of analog signal", "0-10 Volts", _
   Array As String("A", "Bs", "C", "D", "E"))
   
   
    'add the categories to the main screen
    screen.AddPreferenceCategory(cat1)
End Sub

Provided that the above function is called on FirstTime in Activity_Create, I don't find why it fails sometimes.

What I've come up with is:
B4X:
Sub btnSettings_Click
   Try
      StartActivity(screen.CreateIntent)
   Catch
      CreatePreferenceScreen
      StartActivity(screen.CreateIntent)
   End Try

End Sub

In my opinion, that will not trap anything. The manager object lacks a isInitialized() method.
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
seems like an initialization error, but I'm sure that you've checked already such things. Who knows, maybe if you zip/upload your project, we could have a better look...
 
Upvote 0
Top