简体   繁体   English

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

[英]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.我最近开始学习 B4A (Basic For Android),它是一个 IDE,可用于制作 Android 应用程序。

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).错误是:java.lang.RuntimeException: Object 应该首先被初始化(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. Activity_Create 和 Activity_Resume 在应用启动时都会被调用。

You can find out more about the app lifecycle here: https://www.b4x.com/android/forum/threads/android-activity-lifecycle.105551/#post-661112您可以在此处找到有关应用程序生命周期的更多信息: 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 .我还建议你看看使用 B4xPages - https://www.b4x.com/android/forum/threads/b4x-b4xpages-cross-platform-and-simple-framework-for-managing-multiple-pages.118901/后 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. ADDL:您在 Button1_Click 事件子中声明 Label1 和 TBox,请尝试删除它们。 They are already declared in Globals and don't need to be declared again.它们已经在 Globals 中声明,不需要再次声明。

You must first initialize textbox and after that use:您必须首先初始化文本框,然后使用:

textbox.initialize("EventName")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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