User Preferences in a separate Activity

isuru

Member
Licensed User
Longtime User
I'm creating a Settings feature for an app as described here. I put all the code related to the Settings function in a separate Activity and called it from another Activity. But when I do that, I get a runtime error saying the app has stopped unexpectedly. No clear error description is given. Here's the code I've written..

The Main Activity :

B4X:
Sub Process_Globals
   
End Sub

Sub Globals
   Dim pnlAbout As Panel
   Dim btnOk As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   
   Activity.AddMenuItem2("Settings", "settings", LoadBitmap(File.DirAssets, "settings.png"))
   Activity.AddMenuItem2("About", "about", LoadBitmap(File.DirAssets, "info.png"))
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub settings_Click
   StartActivity(Settings.screen.CreateIntent)
End Sub

Sub about_Click
   pnlAbout.Visible = True
End Sub

Sub btnOk_Click
   pnlAbout.Visible = False
End Sub

The Settings Activity :

B4X:
Sub Process_Globals
   Dim manager As PreferenceManager
   Dim screen As PreferenceScreen
End Sub

Sub Globals
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      CreateSettingsScreen
   End If
End Sub

Sub CreateSettingsScreen
   screen.Initialize("Settings", "")
   
   Dim catagory As PreferenceCategory
   catagory.Initialize("")
   catagory.AddCheckBox("Chk", "Auto Search", "Sample desc", False)
   
   screen.AddPreferenceCategory(catagory)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

When I use all the above code in a single Activity, it runs without any problem. It throws the error only when I put it in two separate Activities.

Can anyone please explain why this is happening and how to correct it?

Thank you :)
 

isuru

Member
Licensed User
Longtime User
I'm getting a NullPointerException. But I have no idea what is causing it. I have imported the PreferenceActivity library and defined all the necessary types.

I attached my project here.
 

Attachments

  • MenuExp.zip
    8.4 KB · Views: 272
Upvote 0

Air

Member
Licensed User
Longtime User
I think the error is in the Main Activity.
Check your Sub settings_Click in Main-Activity.

Your Code
B4X:
Sub settings_Click
    StartActivity(Settings.screen.CreateIntent)
End Sub

Try New Code
B4X:
Sub settings_Click
    StartActivity(screen.CreateIntent)
End Sub

I would try this first.
 
Upvote 0

isuru

Member
Licensed User
Longtime User
Can't do that because the variable screen is declared inside the Settings Activity's Process Globals. I have to prefix the Activity name (Settings) to reference its variables.
 
Upvote 0

Air

Member
Licensed User
Longtime User
If you only want to use Preference inside your app but without an extra activity-layout, test my new code.

That´s the way, i use all my preferences in my app.

Sorry, if my englisch is not perfect ;)
 

Attachments

  • New-MenuExp.zip
    8.6 KB · Views: 304
Last edited:
Upvote 0

isuru

Member
Licensed User
Longtime User
Yes, I tried it including in the same activity and it works. But I'm trying to put all the code related to User Preferences into a separate Activity so that to use it in multiple Activities, I only have to reference that single Activity. Otherwise I would have to write the same code to create the preference screen, get and set values and everything in each and every Activity.

Sorry if it sounds confusing.
 
Upvote 0

Air

Member
Licensed User
Longtime User
Did you have soooo much Preference-Entrys?

I have about 10-20 and I use all my Preference Entrys (Set & Get) in the Main-Activity, thats no problem for me ;)
 
Upvote 0

isuru

Member
Licensed User
Longtime User
No, just one. But multiple Activities. This image would explain it more clearer.

bnG8H.png


I'm trying to create one Settings Activity so that other Activities can reference it. Otherwise I would have to write the code for Settings in each Activity. Of course you can do that. But I'm trying to do this to achieve an abstraction level for easy maintainability.
 
Upvote 0

isuru

Member
Licensed User
Longtime User
LogCat connected to: emulator-5554
** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


** Activity (main) Pause, UserClosed = false **


java.lang.RuntimeException: Unable to start activity ComponentInfo{b4a.example/anywheresoftware.b4a.objects.preferenceactivity}: java.lang.NullPointerException


at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
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:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
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:2627)
... 11 more
 
Upvote 0

isuru

Member
Licensed User
Longtime User
I commented out the line screen.Initialize("Settings", "") in the CreateSettingsScreen Sub and added it in the settings_Click event like this

B4X:
Settings.screen.Initialize("Settings", "")
StartActivity(Settings.screen.CreateIntent)

Now no errors are thrown but the Settings UI isn't appearing. :confused:
 

Attachments

  • MenuExp2.zip
    8.5 KB · Views: 270
Upvote 0

isuru

Member
Licensed User
Longtime User
Okay I made some changes. Now it works but I'm not sure if its the correct way to go about it.

The Main Activity :

I just start the Setting Activity from here.

B4X:
Sub Process_Globals
   
End Sub

Sub Globals
   Dim pnlAbout As Panel
   Dim btnOk As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   
   Activity.AddMenuItem2("Settings", "settings", LoadBitmap(File.DirAssets, "settings.png"))
   Activity.AddMenuItem2("About", "about", LoadBitmap(File.DirAssets, "info.png"))
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub settings_Click
   StartActivity(Settings)
End Sub

Sub about_Click
   pnlAbout.Visible = True
End Sub

Sub btnOk_Click
   pnlAbout.Visible = False
End Sub

The Settings Activity :

In the Activity_Create event, I start the Intent.

B4X:
Sub Process_Globals
   Dim manager As PreferenceManager
   Dim screen As PreferenceScreen
End Sub

Sub Globals
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      CreateSettingsScreen
      StartActivity(screen.CreateIntent)
   End If
End Sub

Sub CreateSettingsScreen
   screen.Initialize("Settings", "")
   
   Dim catagory As PreferenceCategory
   catagory.Initialize("")
   catagory.AddCheckBox("Chk", "Auto Search", "Automatically detects your location and display search results", False)
   
   screen.AddPreferenceCategory(catagory)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Can you please just take a look at the code and tell me if its the proper way or not? :)
 

Attachments

  • Exp.zip
    8.5 KB · Views: 286
Upvote 0
Top