German Bitte um Hilfe bei Fehlermeldung

peternmb

Well-Known Member
Licensed User
Longtime User
Hallo,
ich benutze eine kleine Funktion um die Textgröße an ein Label anzupassen.
Das funktioniert in einer App ganz problemlos, in einer anderen App erhalte ich eine Fehlermeldung, bei der ich nicht weiterkomme.
"OK-1" wird mir im Log noch angezeigt, "Ok-2" nicht mehr. Ich kann also den Fehler auf eine Zeile eingrenzen.
Hier meine Funktion:
B4X:
Sub SetTextSize(lblS As Label, txtS As String, Max_Size As Float)
    Private cvs As Canvas
    Private bmp As Bitmap
    '
    Private dt As Float
    Private limit = 0.5 As Float
    Private h1, w As Int
    '
    Private stu As StringUtils
    '
    bmp.InitializeMutable(2dip, 2dip)
    cvs.Initialize2(bmp)
    lblS.Text = txtS
    lblS.TextSize = 300
    dt = lblS.TextSize
    
    Log("OK-1")
    
    h1 = stu.MeasureMultilineTextHeight(lblS, txtS)                 
    
    Log("OK-2")
    
    w = cvs.MeasureStringWidth(txtS, lblS.Typeface, lblS.TextSize)
    '
    Do While dt > limit Or h1 > lblS.Height Or w > lblS.Width/100*92
        dt = dt / 2
        h1 = stu.MeasureMultilineTextHeight(lblS, txtS)
        w = cvs.MeasureStringWidth(txtS, lblS.Typeface, lblS.TextSize)
        
        If h1 > lblS.Height Or w > lblS.Width/100*92 Then
            lblS.TextSize = lblS.TextSize - dt
        Else
            lblS.TextSize = lblS.TextSize + dt
        End If
    Loop
    '
    If lblS.TextSize >  Max_Size Then
        lblS.TextSize = Max_Size
    End If
    '
End Sub
Ich setze die Funktion so ein:
B4X:
        lbl1b.Text =  "Test"       
        Dim Tsize4 As Float  : Tsize=20
        SetTextSize(lbl1b,"Test",Tsize)
Und hier die (gekürzte) Fehlermeldung:
...
OK-1
main_vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3 (java line: 23266)
java.lang.NullPointerException: Attempt to read from field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference
at anywheresoftware.b4a.objects.StringUtils.MeasureMultilineTextHeight(StringUtils.java:51)
at pbit.skat.listenfuehrer2.main._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3(main.java:23266)
at pbit.skat.listenfuehrer2.main._zeig_spiel(main.java:28889)
at pbit.skat.listenfuehrer2.main$ResumableSub_btnSpielOK_Click.resume(main.java:9352)
at pbit.skat.listenfuehrer2.main._btnspielok_click(main.java:8829)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493
...
 

peternmb

Well-Known Member
Licensed User
Longtime User
Leichtsinnsfehler, jetzt selber gefunden.
Natürlich muss das Label erst generiert werden...
B4X:
pnl.AddView(lbl1b, 62%x, 31%y, 30%x, 30%y)
 
Top