B4A 中的错误:java.lang.RuntimeException:应首先初始化对象 (EditText)

问题描述

我最近开始学习 B4A(Android 基础),这是一个可用于制作 Android 应用程序的 IDE。

很遗憾,我在执行代码时遇到错误,我不知道如何解决

错误是:java.lang.RuntimeException: Object should first be initialized (EditText)。

我不知道为什么会出现此错误,请帮忙!

代码

    #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

完整的错误日志:

*** 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)

请帮忙!!!!

解决方法

问题是因为您加载了两次布局。 Activity_Create 和 Activity_Resume 在应用启动时都会被调用。

您可以在此处了解有关应用生命周期的更多信息:https://www.b4x.com/android/forum/threads/android-activity-lifecycle.105551/#post-661112

我还建议您考虑使用 B4xPages - https://www.b4x.com/android/forum/threads/b4x-b4xpages-cross-platform-and-simple-framework-for-managing-multiple-pages.118901/post-743752

即使对于单页应用程序,使用该框架也不会成为生命周期问题。

ADDL:您在 Button1_Click 事件子中声明了 Label1 和 TBox,也尝试删除它们。它们已经在 Globals 中声明,不需要再次声明。