Android Question text file doesn't work

MroBurk

Member
Licensed User
Anyone can Help me with his code? why It doesn't Work???? the error is: for j=0....

B4X:
Sub Process_Globals
    Private xui As XUI
    Dim rp As RuntimePermissions
    
End Sub

Sub Globals
    Dim listresult() As String
    
End Sub

Sub Button1_Click
CreateFile("done1","ok")
End Sub


Sub CreateFile(a as string, b as string)
    
    Dim dir As String  = rp.GetSafeDirDefaultExternal("txt")
    For j=0 To 10
            listresult(j) = "0"
    Next

        listresult(0)= a & "|"& b
        
        File.WriteList(dir,"txt1.txt",listresult)
                
    
End Sub




error code:
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 83 (Main)
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
    at java.lang.reflect.Array.set(Array.java:479)
    at anywheresoftware.b4a.shell.ArraysUtils.setElement(ArraysUtils.java:84)
    at anywheresoftware.b4a.shell.Shell.setArrayElement(Shell.java:591)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:386)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    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:7352)
    at android.widget.TextView.performClick(TextView.java:14230)
    at android.view.View.performClickInternal(View.java:7318)
    at android.view.View.access$3200(View.java:846)
    at android.view.View$PerformClick.run(View.java:27800)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7050)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
 

William Lancee

Well-Known Member
Licensed User
Longtime User
You should also move the declaration to Process_Globals, Globals is usually reserved for Activity structures like Views.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Why not using directly a List?
B4X:
Sub Process_Globals
    Private xui As XUI
    Dim rp As RuntimePermissions
    Dim dir As String  = rp.GetSafeDirDefaultExternal("txt")
    Dim ListResult as List
End Sub

Sub Globals
   
End Sub

Sub Button1_Click
    CreateFile("done1","ok")
End Sub

Sub CreateFile(a as string, b as string)
    ListResult.Initialize
    For j=0 To 10
        ListResult.Add(a & "|" & b)
    Next
    File.WriteList(dir, "txt1.txt", ListResult)
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…