Android Question Login Activity call

Discussion in 'Android Questions' started by rubach, Sep 17, 2018.

  1. rubach

    rubach Member Licensed User

    Hi,
    I am devoloping a project from the this example https://www.b4x.com/android/forum/threads/appcompat-with-toolbar-minimal-example.79896/

    In this case, my app nedd a login activity, this is the code of main activity:

    Code:
    Sub Activity_Create(FirstTime As Boolean)
            
    If (FirstTime) Then
            DBFileDir = 
    File.DirInternal
            DBFileName = clParametros.DbNombre
            
    If File.Exists(DBFileDir, DBFileName) = False Then
                DB.Initialize(DBFileDir, DBFileName, 
    True)
                
    'creo tablas
                
            
    End If
            DB.Initialize(DBFileDir, DBFileName, 
    False)
            
            
    'cargo los parametros
            clParametros.LoadParametros
            
    If clParametros.SessionId.Length=0 Then
                
    'StartActivity(acLogin)
            End If
        
    End If
        
    Log("menu main")
        cartBitmap = 
    LoadBitmap(File.DirAssets, "cart.png")
        
    Activity.LoadLayout("1")
        ToolbarHelper.Initialize
        ToolbarHelper.ShowUpIndicator = 
    True 'set to true to show the up arrow
        ToolbarHelper.UpIndicatorDrawable = BitmapToBitmapDrawable(LoadBitmap(File.DirAssets, "hamburger.png"))
        ACToolBarLight1.InitMenuListener
        sm.Initialize(
    "sm")
        
    Dim offset As Int = 100dip
        sm.BehindOffset = offset
        sm.Mode = sm.LEFT
        
    Dim lftMenu As Panel

        lftMenu.Initialize(
    "")
        sm.Menu.AddView(lftMenu, 
    00100%x - offset, 100%y)
        lftMenu.LoadLayout(
    "Left")
        
    For i = 1 To 30
            ListView1.AddSingleLine(
    "Item " & i)
        
    Next
        
    Dim jo As JavaObject = ACToolBarLight1
        
    Dim xl As XmlLayoutBuilder
        jo.RunMethod(
    "setPopupTheme"Array(xl.GetResourceId("style""ToolbarMenu")))
    End Sub

    Sub Activity_Resume
       
    Log("menu resume")
       
    If clParametros.SessionId.Length=0 Then
           
    StartActivity(acLogin)
       
    End If
    End Sub
    In the loginActivity, after login I call StartActivity(main)


    The problem it is, when I call StartActivity(acLogin) in a Activiti_resume of Main, the activity show the layout of main at firts for a few secconds(1 o 2) and after load the login layout, it is a blink bad efect.

    The other option, it is to call in Main Activity_Create, but in this case, I have some problems:

    1-When I go back to main, after login, the layout show in blank.
    2-If I tryed to load the layout or refresh, in the Activity_Resume, trows an error in null pointer exception in object AcMenu when try to add items of the menu, I think for this part of code

    Code:
    #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
    I belive that I have more than one error, but I don not know what..

    Could any giveme some help to solve the problem? or any example? I review a few examples in the Forum...
    Thank you!
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    It should be better to start the second activity from Activity_Create. You don't need to load the layout in Activity_Resume. Just show the second activity after you loaded the layout.
     
  3. rubach

    rubach Member Licensed User

    Thank you for you answer Erel,
    theh problem it is that not work if I will call the activitity in the Create, trows an error, this is the code:

    Code:
    Sub Activity_Create(FirstTime As Boolean)
        
    'Do not forget to load the layout file created with the visual designer. For example:
       
        
    If (FirstTime) Then
            DBFileDir = 
    File.DirInternal
            DBFileName = clParametros.DbNombre
            
    If File.Exists(DBFileDir, DBFileName) = False Then
                DB.Initialize(DBFileDir, DBFileName, 
    True)
                
    'create tables
            End If
            DB.Initialize(DBFileDir, DBFileName, 
    False)
            
    'load parameters
            clParametros.LoadParametros
            
    If clParametros.SessionId.Length=0 Then
                
    StartActivity(acLogin)
            
    End If
        
    End If
        
    Log("create main")
        cartBitmap = 
    LoadBitmap(File.DirAssets, "cart.png")
        
    Activity.LoadLayout("1")
        ToolbarHelper.Initialize
        ToolbarHelper.ShowUpIndicator = 
    True 'set to true to show the up arrow
        ToolbarHelper.UpIndicatorDrawable = BitmapToBitmapDrawable(LoadBitmap(File.DirAssets, "hamburger.png"))
        ACToolBarLight1.InitMenuListener
        sm.Initialize(
    "sm")
        
    Dim offset As Int = 100dip
        sm.BehindOffset = offset
        sm.Mode = sm.LEFT
        
    Dim lftMenu As Panel

        lftMenu.Initialize(
    "")
        sm.Menu.AddView(lftMenu, 
    00100%x - offset, 100%y)
        lftMenu.LoadLayout(
    "Left")
        
    For i = 1 To 30
            ListView1.AddSingleLine(
    "Item " & i)
        
    Next
        
    Dim jo As JavaObject = ACToolBarLight1
        
    Dim xl As XmlLayoutBuilder
        jo.RunMethod(
    "setPopupTheme"Array(xl.GetResourceId("style""ToolbarMenu")))
       
        
    If StateManager.RestoreState(Activity"Main"60) = False Then
            
    'set the default values
        '    EditText1.Text = "Default text"
        '    EditText2.Text = "Default text"
        End If
       
    End Sub



    Sub ACToolBarLight1_NavigationItemClick
        sm.ShowMenu
    End Sub

    Sub Activity_CreateMenu(Menu As ACMenu)
        
    Menu.Clear
        
    Menu.Add(00"Overflow1"Null)
        
    Menu.Add(00"Overflow2"Null)
        
    Menu.Add(00"Overflow3"Null)
        
    Dim item As ACMenuItem = ACToolBarLight1.Menu.Add2(00"cart"Null)
        item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS
        UpdateIcon(
    "cart", AddBadgeToIcon(cartBitmap, badge))
    End Sub
    The error:
    Error occurred on line: 98 (Main)
    java.lang.NullPointerException: Attempt to read from field 'anywheresoftware.b4a.BA b4a.example.main.activityBA' on a null object reference
    at b4a.example.main._activity_createmenu(main.java:538)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:738)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:357)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at b4a.example.main._onCreateOptionsMenu(main.java:786)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.runHook(BA.java:160)
    at b4a.example.main.onCreateOptionsMenu(main.java:136)
    at android.app.Activity.onCreatePanelMenu(Activity.java:3144)
    at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:364)
    at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
    at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332)
    at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
    at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
    at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:55)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:907)
    at android.view.Choreographer.doCallbacks(Choreographer.java:709)
    at android.view.Choreographer.doFrame(Choreographer.java:641)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:893)
    at android.os.Handler.handleCallback(Handler.java:836)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:203)
    at android.app.ActivityThread.main(ActivityThread.java:6251)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

    I think that this error es because this when execute this code, the layout not are loaded

    Code:
    #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
    Line 98 it is: Sub Activity_CreateMenu(Menu As ACMenu)

    Any idea of how call in Activity_create with out error?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    I see. Try to show it from Activity_CreateMenu. If you are unable to solve it then create a small project and upload it.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice