Android Question Using a Class in a Class impossible?

chrjak

Active Member
Licensed User
Longtime User
Hey Guys. I declare a Class in another Class and somehow i get an error with that code:

B4X:
** Service (starter) Create **
Initialize Service_Starter
** Service (starter) Start **
Start Service_Starter
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 0 (TextSizeHandler_Class)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.keywords.Common.CallSubDebug(Common.java:852)
    at com.hoehenmesser.PlainTap.DEV.refreshservice._refreshtimer_tick(refreshservice.java:161)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:105)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7229)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:336)
    at anywheresoftware.b4a.debug.Debug.CallSubNew(Debug.java:282)
    ... 17 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
    ... 18 more
Caused by: java.lang.NullPointerException: Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference
    at anywheresoftware.b4a.objects.ViewWrapper.getWidth(ViewWrapper.java:140)
    at com.hoehenmesser.PlainTap.DEV.textsizehandler_class._checksize(textsizehandler_class.java:166)
    at com.hoehenmesser.PlainTap.DEV.textsizehandler_class._returntrueiftextsizeistoobigforlabel(textsizehandler_class.java:204)
    at com.hoehenmesser.PlainTap.DEV.label_circlevalue._settext(label_circlevalue.java:91)
    at com.hoehenmesser.PlainTap.DEV.main._circlevalues_update(main.java:1643)
    ... 20 more

Is there maybe a bypass?
 

stevel05

Expert
Licensed User
Longtime User
You need to provide some code that causes the error. It is not possible to understand what you are trying to do from the error log alone.
 
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
You need to provide some code that causes the error. It is not possible to understand what you are trying to do from the error log alone.
Yeah sorry didn't think of this. Here's the code:

In Main I initialize Class 1 with a label
Class 1:
B4X:
'Class module
Sub Class_Globals
    Dim Label As Label
    Dim TextSizeHandler As TextSizeHandler_Class
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(InitializeLabel As Label)
    TextSizeHandler.Initialize
    Label = InitializeLabel
End Sub

Sub getText As String
   Return Label.Text
End Sub

Sub setText(t As String)
    Label.Text = t
    If TextSizeHandler.ReturnTrueIfTextSizeIsTooBigForLabel(Label) Then
        Label.TextSize = TextSizeHandler.ReturnMaximumSizeofTextToFitLabel(Label, False, 80)
    End If  
End Sub
(If i take everything with TextSizeHandler out of class 1 it works)

TextSizeHandler_Class
(I decided to not put this into here to make it easier to overview this message. The 2 used Subs return just values.)
"TextSizeHandler.ReturnTrueIfTextSizeIsTooBigForLabel(Label)" As Boolean
"TextSizeHandler.ReturnMaximumSizeofTextToFitLabel(Label, False, 80)" As Float

Additionally all these subs in TextSizeHandler work when i call them from a Activity. The Error is just from the Class module.

UPDATE: I just thought it could be possible that B4A has problems with "Label" as Label. So i just tested with Label1 but the same error
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It seems that you may be calling setText before initializing this class OR passing an uninitialized label to the initialize function.
 
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
It seems that you may be calling setText before initializing this class OR passing an uninitialized label to the initialize function.
Hmmm interesting idea... Well as I know the labels i initialize with that class are all initialized. But I will look for the other idea... Maybe i call the settext before it is initialized

Update: Nooo that looks all correct O.ô Maybe its really not possible to call a class from a class o.o
 
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
Hey Guys,

I solved this problem somehow. I don't know what was the final solution but i believe that the change from just initializing the label to additionally adding it to activity led to success

Thanks for all your support :)
 
Upvote 0
Top