Android Question Error updating view .text property

SandroB4A

Member
Licensed User
Longtime User
Hi all, I'm trying to localize my app using AndroidResources library.
What I want is to update all views with .text properties using relative .tag property.

I save(manually) the key of xml resource file in the tag property and then I use it for updating the object's strings.
I've prepared a test app with a button, a label and an editext, this is the code:
B4X:
Sub Globals
    Dim AR As AndroidResources
    Dim key As String
End Sub

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

Sub SetLang
    For Each lbl As Label In Activity
        key=lbl.Tag  
        lbl.Text=AR.GetApplicationString(key)  
    Next
    For Each etxt As EditText In Activity
        key=etxt.Tag  
        etxt.Text=AR.GetApplicationString(key)  
    Next  
    For Each btn As Button In Activity
        key=btn.Tag  
        btn.Text=AR.GetApplicationString(key)  
    Next  
  
End Sub

Someone knows why I get this error?

B4X:
** Activity (main) Create, isFirst = false**
main_setlang (java line: 303)

java.lang.ClassCastException: android.widget.Button      
    at test.multilang.main._setlang(main.java:303)
    at test.multilang.main._activity_create(main.java:237)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
    at test.multilang.main.afterFirstLayout(main.java:89)
    at test.multilang.main.access$100(main.java:16)
    at test.multilang.main$WaitForLayout.run(main.java:74)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.ClassCastException: android.widget.Button
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **

P.S. however the value of label, button and edittext are updated fine...
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
For Each iterates over all the views. Not just the Labels (or buttons).

You can use this code:
B4X:
For Each v As View In Activity.GetAllViewsRecursive
 If v Is Label Then
  Dim lbl As Label = v
  lbl.Text = ...
 End If
Next

This exact code will also handle EditTexts, Buttons and other views with text as they all are subclasses of Label.
 
Upvote 0

SandroB4A

Member
Licensed User
Longtime User
Tks Erel, but I've version 2.52 and I haven't .GetAllViewsRecursive so I used this code:

B4X:
For i=0 To Activity.NumberOfViews
    Dim v=activity.GetView(i) As View
    If v Is Label Then
          Dim lbl As Label = v
          lbl.Text = ...
    End If
Next

..and work fine!
thanks again
 
Upvote 0

SandroB4A

Member
Licensed User
Longtime User
Hi Erel, I still have an error.
When a view has a tag without corresponding resource key I get this error:

android.content.res.Resources$NotFoundException: String resource ID #0x7f030003

so I tried to intercept Null value of AndroidResources.GetApplicationString, this is the code:
B4X:
For i=0 To Activity.NumberOfViews
    Dim v=activity.GetView(i) As View
    If v Is Label Then
        Dim lbl As Label= v
        If AR.GetApplicationString(lbl.Tag)=Null Then
            Log("Resource key not found")
        Else
            lbl.Text=AR.GetApplicationString(lbl.Tag)
        End If
    End If
Next

but didn't work!, of course I can use Try/Catch to skip the problem, infact this one works
B4X:
For i=0 To Activity.NumberOfViews
        Dim v=activity.GetView(i) As View
        If v Is Label Then
            Dim lbl As Label= v
            Try
                lbl.Text=AR.GetApplicationString(lbl.Tag)
            Catch
                Log("Resource key not found")
            End Try
        End If
    Next

but I'm not 100% satisfied :D, I'd like to understand why the first code fails, any suggestion?

Tks

Edit.
This one give error
B4X:
Dim key As String = lbl.Tag
If AR.GetApplicationString(key)=Null Then

while this one works:
B4X:
If AR.GetApplicationString("xyz")=Null Then
 
Last edited:
Upvote 0

SandroB4A

Member
Licensed User
Longtime User
I added Key variable to be sure to pass a string to .GetApplicationString.

in my intention Key should have lbl.tag value, something like str0001, str0002... setted in the Designer.

I attach complete code:
B4X:
For i=0 To Activity.NumberOfViews
    Dim v=activity.GetView(i) As View
    If v Is Label Then
      Dim lbl As Label= v
        Dim key As String = lbl.Tag
        If AR.GetApplicationString(key)=Null Then       'This give an error
        'If AR.GetApplicationString("xyz")=Null Then    'with explicit string works
          Log("Resource key not found")
      Else
          lbl.Text=AR.GetApplicationString(lbl.Tag)
      End If
    End If
Next
 
Last edited:
Upvote 0

SandroB4A

Member
Licensed User
Longtime User
Did it, key has values as expected (those defined by me with designer).

Attached you find the project zip file, tks.

Bye
 

Attachments

  • MultiLang.zip
    7.5 KB · Views: 182
Upvote 0

SandroB4A

Member
Licensed User
Longtime User
Yes Erel, the keys do not match the keys of the strings in strings.xml because I want to test the check =Null in that case.

When resource file is ok I've no problem otherwise, as said above, I get this error: android.content.res.Resources$NotFoundException: String resource ID #0x7f030003.

Did you haven't found that error?

I just want a check in case strings.xml isn't complete, and (in my case) AR.GetApplicationString(key)=Null doesn't work.
 
Upvote 0

SandroB4A

Member
Licensed User
Longtime User
Mhmmm...you don't get error as me...:(

I've last version of library and I guess my version of B4A (2.52) is not important.

I don't know why but I must use Try Check statement to go on.

Thank again Erel.

Regards
 
Upvote 0
Top