Android Question Object must first be initialized?

alexwekell

Member
Licensed User
Longtime User
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: True
#End Region

#Region  Project Attributes
    #ApplicationLabel: ActionDrawer Demo
    #VersionCode: 1
    #VersionName: 1.0
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: True
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals

End Sub

Sub Globals
    Dim AD As ActionDrawer
    Dim Label1 As Label
    Dim Label2 As Label
    Dim Label3 As Label
    Dim Label4 As Label
  
    Dim ImageView1 As ImageView
    Dim ImageView2 As ImageView
    Dim ImageView3 As ImageView
    Dim ImageView4 As ImageView
    Dim ImageView5 As ImageView
    Dim ImageView6 As ImageView
    Dim ImageView7 As ImageView
    Dim ImageView8 As ImageView
    Dim ImageView9 As ImageView
  
    Dim RobotoLight As Typeface
    Dim Country As String
End Sub

Sub Activity_Create(FirstTime As Boolean)

    'Overflow Items
    Dim OverflowItems As List
    OverflowItems.Initialize
    OverflowItems.Add("Settings")
    OverflowItems.Add("Help")
    OverflowItems.Add("About")
  
    AD.Initialize("Activity Title", "ic_icon.png", True, Colors.RGB(213,213,213), "light", True, True, False, OverflowItems, "Drawer", Activity, Me)
    'If you want to use a light theme add "SetApplicationAttribute(android:theme, "@android:style/Theme.Holo.Light")" to the manifest
  
    AD.AppTitle = "App Title"
  
    AD.AddAction("Search", "Search_Click", "ic_search.png")  
  
    'If you set drawer to false in AD.Initialize and try to add a view to the drawer you will get an error
    Dim lInfo As Label
    lInfo.Initialize("")
    lInfo.Text = "ActionDrawer Demo"
    'AD.DrawerPanel.AddView(lInfo, 4dip, 0, AD.DrawerPanel.Width, 100dip)
  
    'If you are using the Navigation Drawer you will need to add your views to AD.pContent rather than Activity
    'In this example I make another panel to add everything to. This will save typing time in large projects
    'Dim Activ As Panel
    'Activ.Initialize("")
    'AD.pContent.AddView(Activ, 0, 0, 100%x, 100%y)
    'Activ.SendToBack
  
    Dim lText As Label
    lText.Initialize("")
    lText.Color = Colors.White
    lText.Text = CRLF & "  Welcome to" & CRLF & "  ActionDrawer"
    lText.TextSize = 28
    'Activ.AddView(lText, 0, 0, 100%x, 100%y)
  

Activity.LoadLayout("main")

    RobotoLight = Typeface.LoadFromAssets("Roboto-Light.ttf")
  
  
  
    SetNinePatchDrawable(Label1, "label_bg")
    Label1.Typeface = RobotoLight
  
    SetNinePatchDrawable(Label2, "label_bg")
    Label2.Typeface = RobotoLight
  
    SetNinePatchDrawable(Label3, "label_bg")
    Label3.Typeface = RobotoLight
  
    SetNinePatchDrawable(Label4, "label_bg")
    Label4.Typeface = RobotoLight
  

End Sub

Sub SetNinePatchDrawable(Control As View, ImageName As String)
    Dim r As Reflector
    Dim package As String
    Dim id As Int
    package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
    id = r.GetStaticField(package & ".R$drawable", ImageName)
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    Control.Background = r.RunMethod2("getDrawable", id, "java.lang.int")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Up_Click
    ToastMessageShow("Up Clicked", False)
End Sub

Sub Search_Click
Dim Id As InputDialog
Id.Input=""
Id.Hint = ""
    ret = DialogResponse.CANCEL
    ret = Id.Show("Enter the name of the country:", "Country Search", "Search", "", "", Null)
    Country = Id.Input
  
    If Country.EqualsIgnoreCase("Algeria") = True Then
    Label1.Text = "        14"
    Label3.Text = "        14"
    Label4.Text = "        17"
    End If
    If Country.EqualsIgnoreCase("Botswana") = True Then
    Label1.Text = "    911"
    End If
        If Country.EqualsIgnoreCase("Cameroon") = True Then
    Label1.Text = "    P: 17 F: 18"
    End If
        If Country.EqualsIgnoreCase("Chad") = True Then
    Label1.Text = "    P: 17 F: 18"
    End If
        If Country.EqualsIgnoreCase("Botswana") = True Then
    Label1.Text = "    911"
    End If
        If Country.EqualsIgnoreCase("Botswana") = True Then
    Label1.Text = "    911"
    If Country.EqualsIgnoreCase("Botswana") = True Then
    Label1.Text = "    911"
    End If
        If Country.EqualsIgnoreCase("Botswana") = True Then
    Label1.Text = "    911"

    End If
    End If
  

End Sub

Sub Copy_Click
    ToastMessageShow("Copy Clicked", False)
End Sub

Sub Edit_Click
    ToastMessageShow("Edit Clicked", False)
End Sub

Sub Delete_Click
    ToastMessageShow("Delete Clicked", False)
End Sub

Sub Add_Item_Click
    ToastMessageShow("Add Item Clicked", False)
End Sub

Sub ImageView1_Click
    Dim i As PhoneCalls
StartActivity(i.Call(Label1.Text.Trim))
End Sub

Sub Overflow_Click(Item As String)
    Select Item
        Case "Settings"
            ToastMessageShow("Settings Clicked", False)
        Case "Help"
            ToastMessageShow("Help Clicked", False)
        Case "About"
            ToastMessageShow("About Clicked", False)
    End Select
End Sub

Compiles fine, but when ran, I get this error:

B4X:
LogCat connected to: B4A-Bridge: LGE Nexus 5-358239053304616
Installing file.
--------- beginning of /dev/log/main
running waiting messages (2)
** Activity (main) Resume **
Connected to B4A-Bridge (Wifi)
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:actn.drwr
** Activity (main) Create, isFirst = true **
(Main, 85) FullScreen or IncludeTitle properties in layout file do not match the activity attributes settings. (warning #1004)
main_activity_create (B4A line: 85)
Activity.LoadLayout("main")
java.lang.RuntimeException: java.lang.RuntimeException: Object should first be initialized (ImageView).
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:151)
    at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:191)
    at actn.drwr.main._activity_create(main.java:309)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at actn.drwr.main.afterFirstLayout(main.java:89)
    at actn.drwr.main.access$100(main.java:16)
    at actn.drwr.main$WaitForLayout.run(main.java:74)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Object should first be initialized (ImageView).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:46)
    at anywheresoftware.b4a.objects.ViewWrapper.innerInitialize(ViewWrapper.java:56)
    at anywheresoftware.b4a.objects.ImageViewWrapper.innerInitialize(ImageViewWrapper.java:35)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:285)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:300)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:129)
    ... 18 more
java.lang.RuntimeException: java.lang.RuntimeException: Object should first be initialized (ImageView).
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **

What am I doing wrong...?
 

alexwekell

Member
Licensed User
Longtime User
It seems you are missing an ImageView, the error points to Line 85 on your code, check your layout.

I see that it is a reference to a missing ImageView, but I have everything? Don't I?
 

Attachments

  • Capture.JPG
    59.5 KB · Views: 295
  • Capture.JPG
    59.5 KB · Views: 274
Upvote 0

Mrjoey

Active Member
Licensed User
Longtime User
U should call activity.loadlayout before the labels assignements , just make it the first call and then add views u want
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
No, views are initialized by the Designer, you should only declare them in your code, and alex did it.

Also, you can't name a variable (or view) like "ImageView", so the message errore is strange.
______________________________________________________________________________
I was writing... and writing not noticing the lack of LoadLayout. Great observer, Luca
 
Upvote 0

alexwekell

Member
Licensed User
Longtime User
I guess I fixed it, the program couldn't find some imagefiles when I had updated them so I just went through and reassigned my image views
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…