简体   繁体   中英

Error in B4A: java.lang.RuntimeException: Object should first be initialized (EditText)

I have recently started to learn B4A (Basic For Android) which is an IDE that can be used to make Android apps.

Unfortunately, I got an error while I was executing my code and I do not know how to fix it.

The error is: java.lang.RuntimeException: Object should first be initialized (EditText).

I have no idea why I am getting this error please help!

Code:

    #Region  Project Attributes 
    #ApplicationLabel: Chatbot
    #VersionCode: 1
    #VersionName: 
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Public Label1 As Label
    Public TBox As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Private Label1 As Label
    Private TBox As EditText
    If TBox.Text = "hello" Then 
        Label1.Text = "Hi!"
    Else 
        Label1.Text = "Invalid Command"
    End If
    End Sub

Full Error Log:

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_button1_click (java line: 372)
java.lang.RuntimeException: Object should first be initialized (EditText).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
    at anywheresoftware.b4a.objects.TextViewWrapper.getText(TextViewWrapper.java:36)
    at b4a.example.main._button1_click(main.java:372)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:5638)
    at android.view.View$PerformClick.run(View.java:22430)
    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:6198)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)

Please Help!!!!

The problem is caused because you are loading the layout twice. Both Activity_Create and Activity_Resume are called when the app starts.

You can find out more about the app lifecycle here: https://www.b4x.com/android/forum/threads/android-activity-lifecycle.105551/#post-661112

I also suggest that you look at using B4xPages - https://www.b4x.com/android/forum/threads/b4x-b4xpages-cross-platform-and-simple-framework-for-managing-multiple-pages.118901/post-743752 .

Lifecycle issues will be less of a problem using that framework even for a single page app.

ADDL: You are declaring Label1 and TBox in the Button1_Click Event sub, try removing these as well. They are already declared in Globals and don't need to be declared again.

You must first initialize textbox and after that use:

textbox.initialize("EventName")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM