Android Question AppCompat Random crash on CreateMenu

techknight

Well-Known Member
Licensed User
I keep having these sporatic random crashes that I cannot seem to narrow down.

I mean this is very very intermittent. only once in awhile when the App starts, this crash happens. Seems to be happening when it tries to create the menu using the CreateMenu subroutine.

Here is the crash log, and my subroutine:

B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (scoreengine) Create ***
** Service (scoreengine) Start **
releasing KeepAlive
No wakelock.
turning screen on
using partialLock
*** Service (commengine) Create ***
** Service (commengine) Start **
---- AppUpdating.ReadCurVN
UpdateComplete - time: 11:41:37
Status: -1
** Activity (main) Pause, UserClosed = false **
Cant read CommEngine variables
main_activity_createmenu (java line: 588)
java.lang.NullPointerException: Attempt to read from field 'de.amberhome.objects.appcompat.ACToolbarLightWrapper md.smfootball1a.main._actoolbarlight1' on a null object reference
    at md.smfootball1a.main._activity_createmenu(main.java:588)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at md.smfootball1a.main._onCreateOptionsMenu(main.java:6710)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.runHook(BA.java:165)
    at md.smfootball1a.main.onCreateOptionsMenu(main.java:136)
    at android.app.Activity.onCreatePanelMenu(Activity.java:3324)
    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:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6823)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
java.lang.NullPointerException: Attempt to read from field 'de.amberhome.objects.appcompat.ACToolbarLightWrapper md.smfootball1a.main._actoolbarlight1' on a null object reference
** Activity (main) Resume **
B4X:
Sub Activity_CreateMenu(Menu As ACMenu)
    Menu.Clear
    Try 'If the CommEngine Service has not been constructed yet, this routine will crash.
        Select Case CommEngine.connected
            Case True
                Dim Item As ACMenuItem = ACToolBarLight1.Menu.Add2(0, 0, "mnuConnection", Null)
                Item.ShowAsAction = Item.SHOW_AS_ACTION_ALWAYS
                UpdateIcon("mnuConnection", LoadBitmap(File.DirAssets, "power-green.png"))
            Case False
                Dim Item As ACMenuItem = ACToolBarLight1.Menu.Add2(0, 0, "mnuConnection", Null)
                Item.ShowAsAction = Item.SHOW_AS_ACTION_ALWAYS
                UpdateIcon("mnuConnection", LoadBitmap(File.DirAssets, "power-red.png"))
        End Select
    Catch
        Log("Cant read CommEngine variables")
        Dim Item As ACMenuItem = ACToolBarLight1.Menu.Add2(0, 0, "mnuConnection", Null)
        Item.ShowAsAction = Item.SHOW_AS_ACTION_ALWAYS
        UpdateIcon("mnuConnection", LoadBitmap(File.DirAssets, "power-red.png"))
    End Try
    Dim Item As ACMenuItem = ACToolBarLight1.Menu.Add2(0, 0, "mnuClear", Null)
    Item.ShowAsAction = Item.SHOW_AS_ACTION_ALWAYS
    UpdateIcon("mnuClear", LoadBitmap(File.DirAssets, "trash.png"))
    Dim Item As ACMenuItem = ACToolBarLight1.Menu.Add2(0, 0, "mnuSetClock", Null)
    Item.ShowAsAction = Item.SHOW_AS_ACTION_ALWAYS
    UpdateIcon("mnuSetClock", LoadBitmap(File.DirAssets, "stopwatch.png"))
    Dim Item As ACMenuItem = ACToolBarLight1.Menu.Add2(0, 0, "mnuTimeOfDay", Null)
    Item.ShowAsAction = Item.SHOW_AS_ACTION_ALWAYS
    UpdateIcon("mnuTimeOfDay", LoadBitmap(File.DirAssets, "clock.png"))
End Sub
Any thoughts?
 
Last edited:

DonManfred

Expert
Licensed User
ACToolBarLight1.Menu
why are you using this construct?
The event has the Menu as a parameter. I suggest to just use THIS Menu Object.
Dim Item As ACMenuItem = ACToolBarLight1.Menu.Add2(0, 0, "mnuConnection", Null)
B4X:
Dim Item As ACMenuItem = Menu.Add2(0, 0, "mnuConnection", Null)
 

techknight

Well-Known Member
Licensed User
I dont know why. I will have to go back and look again. Its probably some dumb mistake I did somewhere.

Edit: I think its because I found it in example code somewhere, and there is a Menu object for ACToolBarLight1 which is the toolbar

Edit2: Also I am still used to old school programming, the variable passed in being just a copy and I dont manipulate those at all. (In my mind as an old schooler)

Old habits die hard!
 
Last edited:

techknight

Well-Known Member
Licensed User
Issue remains unresolved.

Just had it randomly happen again today, in my other app. I removed the ACtoolbarlight reference and just use the menu passed through there. Same crash.

B4X:
Logger connected to:  samsung SM-T377V
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (scoreengine) Create ***
** Service (scoreengine) Start **
releasing KeepAlive
No wakelock.
turning screen on
using partialLock
*** Service (commengine) Create ***
** Service (commengine) Start **
---- AppUpdating.ReadCurVN
UpdateComplete - time: 14:08:54
---- AppUpdating.ReadWebVN
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
---- AppUpdating.JobDone --
UpdateComplete - time: 14:08:54
** Activity (main) Pause, UserClosed = false **
Cant read CommEngine variables
main_vvvvvvvvvvvvvvvvvvvvvvvvv3 (java line: 3148)
java.lang.NullPointerException: Attempt to read from field 'de.amberhome.objects.appcompat.ACToolbarLightWrapper md.smbaseball2a.main._actoolbarlight1' on a null object reference
    at md.smbaseball2a.main._vvvvvvvvvvvvvvvvvvvvvvvvv3(main.java:3148)
    at md.smbaseball2a.main._vvvvvvvvvvvvvvvvvvvvvv2(main.java:6693)
    at md.smbaseball2a.main._activity_createmenu(main.java:625)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at md.smbaseball2a.main._onCreateOptionsMenu(main.java:7077)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.runHook(BA.java:165)
    at md.smbaseball2a.main.onCreateOptionsMenu(main.java:136)
    at android.app.Activity.onCreatePanelMenu(Activity.java:3324)
    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:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6823)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
java.lang.NullPointerException: Attempt to read from field 'de.amberhome.objects.appcompat.ACToolbarLightWrapper md.smbaseball2a.main._actoolbarlight1' on a null object reference
** Activity (main) Resume **
Here is my menu code currently:

B4X:
Sub Activity_CreateMenu(Menu As ACMenu)
    Menu.Clear
    Try 'If the CommEngine Service has not been constructed yet, this routine will crash. 
        Select Case CommEngine.connected
            Case True
                Dim Item As ACMenuItem = Menu.Add2(0, 0, "mnuConnection", Null)
                Item.ShowAsAction = Item.SHOW_AS_ACTION_ALWAYS
                UpdateIcon("mnuConnection", LoadBitmap(File.DirAssets, "power-green.png"))
            Case False
                Dim Item As ACMenuItem = Menu.Add2(0, 0, "mnuConnection", Null)
                Item.ShowAsAction = Item.SHOW_AS_ACTION_ALWAYS
                UpdateIcon("mnuConnection", LoadBitmap(File.DirAssets, "power-red.png"))
        End Select
    Catch
        Log("Cant read CommEngine variables")
        Dim Item As ACMenuItem = Menu.Add2(0, 0, "mnuConnection", Null)
        Item.ShowAsAction = Item.SHOW_AS_ACTION_ALWAYS
        UpdateIcon("mnuConnection", LoadBitmap(File.DirAssets, "power-red.png"))
    End Try
    Dim Item As ACMenuItem = Menu.Add2(0, 0, "mnuClear", Null)
    Item.ShowAsAction = Item.SHOW_AS_ACTION_ALWAYS
    UpdateIcon("mnuClear", LoadBitmap(File.DirAssets, "trash.png"))
    Dim Item As ACMenuItem = Menu.Add2(0, 0, "mnuSetClock", Null)
    Item.ShowAsAction = Item.SHOW_AS_ACTION_ALWAYS
    UpdateIcon("mnuSetClock", LoadBitmap(File.DirAssets, "stopwatch.png"))
    Dim Item As ACMenuItem = Menu.Add2(0, 0, "mnuTimeOfDay", Null)
    Item.ShowAsAction = Item.SHOW_AS_ACTION_ALWAYS
    UpdateIcon("mnuTimeOfDay", LoadBitmap(File.DirAssets, "clock.png"))
End Sub
thoughts?
 

techknight

Well-Known Member
Licensed User
With some more digging, I think I need to fix the GetMenuItem subroutine as well. you think?

Because inside the main.java file, I found the line causing the crash:
B4X:
public static de.amberhome.objects.appcompat.ACMenuItemWrapper  _vvvvvvvvvvvvvvvvvvvvvvvvv3(String _title) throws Exception{
int _i = 0;
de.amberhome.objects.appcompat.ACMenuItemWrapper _m = null;
 //BA.debugLineNum = 2686;BA.debugLine="Sub GetMenuItem(Title As String) As ACMenuItem";
 //BA.debugLineNum = 2687;BA.debugLine="For i = 0 To ACToolBarLight1.Menu.Size - 1";
{
final int step1 = 1;
final int limit1 = (int) (mostCurrent._actoolbarlight1.getMenu().Size()-1); <---Offending Line
_i = (int) (0) ;
for (;_i <= limit1 ;_i = _i + step1 ) {
 //BA.debugLineNum = 2688;BA.debugLine="Dim m As ACMenuItem = ACToolBarLight1.Menu.GetIt";
_m = new de.amberhome.objects.appcompat.ACMenuItemWrapper();
_m = mostCurrent._actoolbarlight1.getMenu().GetItem(_i);
 //BA.debugLineNum = 2689;BA.debugLine="If m.Title = Title Then";
if ((_m.getTitle()).equals(_title)) { 
 //BA.debugLineNum = 2690;BA.debugLine="Return m";
if (true) return _m;
 };
 }
};
 //BA.debugLineNum = 2693;BA.debugLine="Return Null";
if (true) return (de.amberhome.objects.appcompat.ACMenuItemWrapper) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new de.amberhome.objects.appcompat.ACMenuItemWrapper(), (android.view.MenuItem)(anywheresoftware.b4a.keywords.Common.Null));
 //BA.debugLineNum = 2694;BA.debugLine="End Sub";
return null;
}
Which is this sub:

B4X:
Sub GetMenuItem(Title As String) As ACMenuItem
    For i = 0 To ACToolBarLight1.Menu.Size - 1
        Dim m As ACMenuItem = ACToolBarLight1.Menu.GetItem(i)
        If m.Title = Title Then
            Return m
        End If
    Next
    Return Null
End Sub
I think that sub came from one of Erel's example apps.
 

techknight

Well-Known Member
Licensed User
Soooo.... I modified the subroutines to this:

B4X:
Sub UpdateIcon(MenuTitle As String, Icon As Bitmap, Menu As ACMenu)
    Dim m As ACMenuItem = GetMenuItem(MenuTitle, Menu)
    m.Icon = BitmapToBitmapDrawable(Icon)
End Sub
Sub GetMenuItem(Title As String, Menu As ACMenu) As ACMenuItem
    For i = 0 To Menu.Size - 1
        Dim m As ACMenuItem = Menu.GetItem(i)
        If m.Title = Title Then
            Return m
        End If
    Next
    Return Null
End Sub
I did this to see if this would alleviate the problem. Good news is, I didnt break anything. But I havent had a crash yet either because its random. Ill keep yall posted if something happens.
 
Top