Android Question Activity_CreateMenu And Translation

BitsAndBytes

Active Member
Licensed User
I have one app with 2 activities one Main and one with the option to change language. When my app stats i create a menu but i want when the user change language so do menu to change language each time the activity is resuming ..

B4X:
#IgnoreWarnings: 11
Sub Activity_CreateMenu(menu As ACMenu)
   menu.Clear
   menu.Add(1,0,Module.MenuTranslate(0),Null)
   menu.Add(2,0,Module.MenuTranslate(1),Null)
End Sub

Sub RefreshMenu()
   acactionbar.Menu.Clear
   acactionbar.menu.Add(1,0,Module.MenuTranslate(0),Null)
   acactionbar.menu.Add(2,0,Module.MenuTranslate(1),Null)
End Sub

But each time my app try to refresh my menu and load the new translation i receive that error...

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Panel size is unknown. Layout may not be loaded correctly.
** Activity (main) Pause, UserClosed = false **
main_activity_createmenu (java line: 476)
java.lang.NullPointerException
at agricultural.app.main._activity_createmenu(main.java:476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:179)
at agricultural.app.main._onCreateOptionsMenu(main.java:2038)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.runHook(BA.java:152)
at agricultural.app.main.onCreateOptionsMenu(main.java:134)
at android.app.Activity.onCreatePanelMenu(Activity.java:2508)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:362)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

Thank you
 

BitsAndBytes

Active Member
Licensed User
I found the problem here ...
B4X:
   dim strMenuItemTitle as string = "Menu Title 1"
    Menu.Add(0, 0, strMenuItemTitle , Null)

When i put the strMenuItemTitle my app crashes but when i use the code below everything works fine ..

B4X:
    Menu.Add(0, 0, "Menu Title 1", Null)

How i can convert String To Charsequence? Thank you
 
Upvote 0

BitsAndBytes

Active Member
Licensed User
B4X:
  Dim charSequences As CSBuilder
  charSequences.Initialize
  menu.Clear
  menu.Add(1, 0, charSequences.Append(Module.arrCategoriesPanel(0)), Null)

The Module.arrCategoriesPanel(0)) just has a string value like "Menu Option 1" but this Does not work
 
Upvote 0

Misterbates

Active Member
Licensed User
Could you post the error? Don't forget to post it inside a code or quote block ...
And could you post your current code (including sub/end sub surrounds) - getting one or two lines may not be enough to see the bigger picture
 
Upvote 0

BitsAndBytes

Active Member
Licensed User
B4X:
#IgnoreWarnings: 11
Sub Activity_CreateMenu(menu As ACMenu)  
   Dim dummy As CSBuilder
   dummy.Initialize

   Dim cs() As CSBuilder = Array As CSBuilder(dummy)
   cs(0).Initialize.Append(Module.arrCategoriesPanel(0))  

   menu.Clear  
   menu.Add(1, 0, cs(0), Null)
End Sub

The problem is that i need to convert Module.arrCategoriesPanel(0) which is a string with value "Menu Option 1" , to charSequence
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You DONT need multiple CSBuilders. See CSBuilder Tutorial.

Declare one GLOBAL and use the same every time...

Something like
B4X:
 Dim cs As CSBuilder ' Global var....
menu.add(cs.Initialize.Append(Module.arrCategoriesPanel(0)).PopAll, null)
 
Last edited:
Upvote 0

BitsAndBytes

Active Member
Licensed User
The same error again with this code

B4X:
#IgnoreWarnings: 11
Sub Activity_CreateMenu(menu As ACMenu)
      dim strText as String
      strText = "I am a simple text message, please work!!!"
      menu.Clear
      menu.add(1, 0, cs.Initialize.Append(strText).PopAll, Null)
End sub

Thats the error again ...

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
main_activity_createmenu (java line: 502)
java.lang.NullPointerException
at agricultural.app.main._activity_createmenu(main.java:502)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:179)
at agricultural.app.main._onCreateOptionsMenu(main.java:2044)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.runHook(BA.java:152)
at agricultural.app.main.onCreateOptionsMenu(main.java:134)
at android.app.Activity.onCreatePanelMenu(Activity.java:2508)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:362)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

if i use this code below every think works fine, what is going on
B4X:
#IgnoreWarnings: 11
Sub Activity_CreateMenu(menuAs ACMenu)
     menu.Clearmenu.add(1, 0, ""I am a simple text message, please work!!!", Null)
End sub

my manifest is <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19"/>

i am using this C:\android-sdk-windows\platforms\android-26\android.jar

on an android device with 4.2.2 API 17
 
Last edited:
Upvote 0

Misterbates

Active Member
Licensed User
Try the following:
1) Clean your project (Ctrl+P)
2) Export your project as zip (file menu)
3) Delete (or rename) your project folder
4) Unzip from the export to a folder with the same name as you were using
5) Open and compile.

I've found that sometimes errors like this are fixed by using a "clean" environment.
 
Upvote 0

Misterbates

Active Member
Licensed User
Another idea...

In my (working) implementation I have the following to create the initial menu:
B4X:
#If Java

public boolean _onCreateOptionsMenu(android.view.Menu menu) {
    if (processBA.subExists("activity_createmenu")) {
        processBA.raiseEvent2(null, true, "activity_createmenu", false, new de.amberhome.objects.appcompat.ACMenuWrapper(menu));
        return true;
    }
    else
        return false;
}
#End If

#IgnoreWarnings: 11
Sub Activity_CreateMenu(Menu As ACMenu)
    Menu.Clear
    Dim item As ACMenuItem
    ' Add Create folder item - number < 100 so it's never removed.
    Menu.Add2(10, 1, moResStrings.Get(sRES_TOOLBAR_CREATEFOLDER), modUtility.GetDrawable("ic_folder_add_black_24dp", moIcons)).ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
    ' Add other menu items (under a More button)
    UpdateMenu(Menu, True)
End Sub

I have the following to update the menu (called from Activity_Resume and a few other places in my code)
B4X:
private Sub UpdateDisplay
    Dim oMenuItem As ACMenuItem
    ' Clear existing dynamic items (if any)
    If barAction.Menu.Size > 0 Then
        oMenuItem = barAction.Menu.GetItem(barAction.Menu.Size - 1)
        Do While oMenuItem.Id >= 100
            barAction.Menu.RemoveItem(oMenuItem.Id)
            oMenuItem = barAction.Menu.GetItem(barAction.Menu.Size - 1)
        Loop
    End If
    ' Setup toolbar and menu items depending on whether we're showing root folder or a sub folder
    If goStorage.sCurrentFolder = goStorage.sRootFolder Then
        barHelper.ShowUpIndicator = False
        barAction.Title = Application.LabelName
        barAction.SubTitle = ""
        UpdateMenu(barAction.Menu, True)
    Else
        barHelper.ShowUpIndicator = True  
        barAction.Title = goStorage.sCurrentFolderName
        barAction.SubTitle = goStorage.sCurrentFolderWorkouts
        UpdateMenu(barAction.Menu, False)
    End If
End Sub

... and I have the following sub:
B4X:
private Sub UpdateMenu(Menu As ACMenu, bRoot As Boolean)
    Dim iMenu As Int = 100
    If bRoot Then
        For Each sItem As String In Array As String(moResStrings.Get(sRES_TOOLBAR_SETTINGS), _
                                                    moResStrings.Get(sRES_TOOLBAR_EXAMPLES), _
                                                    moResStrings.Get(sRES_TOOLBAR_HELP), _
                                                    moResStrings.Get(sRES_TOOLBAR_PURCHASE))
            Menu.Add(iMenu, iMenu, sItem, Null)
            iMenu = iMenu + 1
        Next
    Else
        For Each sItem As String In Array As String(moResStrings.Get(sRES_TOOLBAR_FOLDERRENAME), _
                                                    moResStrings.Get(sRES_TOOLBAR_FOLDERMOVE), _
                                                    moResStrings.Get(sRES_TOOLBAR_FOLDERCOPY), _
                                                    moResStrings.Get(sRES_TOOLBAR_FOLDERDELETE), _
                                                    moResStrings.Get(sRES_TOOLBAR_FOLDERSORTBY))
            Menu.Add(iMenu, iMenu, sItem, Null)
            iMenu = iMenu + 1
        Next
    End If
End Sub

As you can see, I'm setting the menu text with a string. But looking at my code, I can also see that code to clear the menu or to remove existing menu items is separated from code to add menu items. And I recall that when I tried to add the "Do While oMenuItem.Id >= 100" loop in the second segment into the "UpdateMenu" sub, I started to get weird exceptions.

So if the "clean it" idea doesn't work, try separating your menu.clear from your menu.add code.
 
Upvote 0

BitsAndBytes

Active Member
Licensed User
I found the solution finaly and maybe this will help to any user who want to add translated menus!
B4X:
Private acMenuPopup As ACPopupMenu

#IgnoreWarnings: 11
Sub Activity_CreateMenu(menu As ACMenu)  
   Dim item As ACMenuItem
   acActionBarLight.Menu.Add2(1,1,"show", xml.GetDrawable("ic_settings_24dp")).ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
End Sub

Sub refreshMenu
   acMenuPopup.Initialize("acMenuPopup", dummypanel)  
   acMenuPopup.AddMenuItem(1,Module.arrCategoriesPanel(0),Null)
   acMenuPopup.AddMenuItem(2,Module.MenuTranslate(1),Null)
   acMenuPopup.AddMenuItem(3,Module.MenuTranslate(2),Null)
End Sub

Sub acMenuPopup_ItemClicked (item As ACMenuItem)
   Select item.Id
     Case 1
       StartActivity("settings")
     Case 2
       StartActivity("backup")
     Case 3
       StartActivity("about")
   End Select
End Sub

Sub hideMainMenu
   acActionBarLight.Menu.Clear
End Sub

Sub acActionBarLight_MenuItemClick (Item As ACMenuItem)
   Select Case Item.Id
     Case 1
       acMenuPopup.Show
   End Select
End Sub
Everything works fine now , thanks to your support
Thank you
 
Last edited:
Upvote 0

Misterbates

Active Member
Licensed User
No worries. In your solution code, how does the popup menu get shown (isn't there a "actionbar_MenuItemClick" sub somewhere)? And the hideMainMenu sub, shouldn't that be clearing the popup menu, not the acActionBarLight menu?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…