onDraw wird nicht aufgerufen

AquaBall

Top Contributor
Ich habe ein seltsames Problem:
Mein selbst erstelltes CustomView funktioniert eigentlich wunderbar.

Im "root"-Layout(ScrollView) wird ein FrameLayout eingebettet, in das (im wesentlichen) ein (dynamisches) ImageView enthält.

Nun ist bekannt, dass bei onCreate und onResume die methoden getWidth() noch nichts zurückgeben.
Erst bei onDraw() oder onSizeChanged() der neuen View-classe ist die Größe bekannt. (Siehe auch http://www.java-forum.org/mobile-ge...uredwidth-gibt-nichts-zurueck.html#post900901)
Soweit funktioniert es auch.

Wenn ich mein FrameLayout nun aber noch in ein TableRow tiefer einbette, dann werden onDraw() oder onSizeChanged() nicht mehr aufgerufen.

Über rekursives getChildAt() stellt sich aber heraus, dass meinCustomView an der richtigen Stelle reingehängt ist, aber einfach keine Größe liefert.

Warum wird onDraw() nicht aufgerufen?
 

schlingel

Gesperrter Benutzer
Wird es denn angezeigt?

Wenn das System die View nicht am Screen anzeigen muss, dann wird's auch nicht gezeichnet. Kann es überhaupt am Screen erscheinen bei deiner Konfiguration?
 

AquaBall

Top Contributor
Das Problem ist, dass es NICHT erscheint. Deshalb wird auch kein onDraw aufgerufen. Und solange kein onDraw aufgerufen wird, so lang kenn ich die Breites des Frames nicht, und solange kann ich kein Image korrekt erstellen. (Auch ein "vorläufiges" statisches Erstellen eines Bildes mit fixer Breite - die sicher Platz hat - funktioniert nicht.)

Das CustomView mit dynamischem Bild erscheint korrekt bei: (PseudoCode)
scrollView.addView(frameLayout);
frameLayout.addView(customView);

Es erscheint auch korrekt bei statischer Programmierung: (XML)
<scrollView ...>
<tableRow ...>
<frameLayout ...>
<customView ...>

Es erscheint aber gar NICHT (und deshalb auch kein onDraw):
scrollView.addView(tableRow);
tableRow.addView(frameLayout);
frameLayout.addView(customView);

Obwohl bei allen Varianten ein rekursives getChildAt() den richtigen/gewünschten Aufbau der Layouts bestätigt.
 
Zuletzt bearbeitet:

schlingel

Gesperrter Benutzer
Mich wundert eher dass es beim XML erscheint als das es im Java-Code nicht erscheint.

Aus der Doku von TableRow:
A layout that arranges its children horizontally. A TableRow should always be used as a child of a TableLayout. If a TableRow's parent is not a TableLayout, the TableRow will behave as an horizontal LinearLayout.

Kann es sein, dass deine TableRow das FrameLayout aus dem Viewport hinausschiebt weil noch andere Elemente enthalten sind? Warum verwendest du überhaupt ein TableRow?
 

AquaBall

Top Contributor
Meine bisherige Konstellation ist: ScreenBreite (ohne Padding 480)
[XML]ScrollView
TableRow
TextView | EdixText | TextView // Gesamtbreite ca 150
frameLayout
ImageView + ImageView //=CustomView
[/XML]

Ich hätte aber gerne das CustomView neben dem 2. TextView, weil (wenn) genügend Platz ist:
[XML]
ScrollView
TableRow
TextView | EdixText | TextView | frameLayout
ImageView + ImageView //=CustomView
[/XML]

"Rausgeschoben" kann irgendwie nicht sein.
Wie gesagt, bei statischer Programmierung passt's,
bei dynamischer passen zwar die Daten, aber es erscheint nichts.

ABer ich werd mal testen, obs am (fehlende) tableLayout liegt.
 

schlingel

Gesperrter Benutzer
An deiner Stelle würde ich mir noch einmal genau überlegen was du machen möchtest und noch einmal beim Aufbau deiner View-Hierarchie anfangen.

Das wirkt zurzeit wirr und vor allem nicht auf verschiedene Breiten angepasst.
 

AquaBall

Top Contributor
So, nun hab ich es auch mit TableRow getestet: Kein Unterschied.
Am meisten stört mich, dass dass 2. TextView an Stelle 143 erscheint und 40 breit ist, das darauffolgende FramLayout aber an Stelle 0 beginnt, und 0 breit ist
(134 und 117 ist y am Bildschirm)
HTML:
1--class android.widget.TextView    'eh_h'   = 143+40/134   : 0
1--class android.widget.FrameLayout 'fr_h'   = 0+0/117   : 0
Legend:
RekursionsEbene: 1--
ViewClasse: class android.widget.FrameLayout
ViewTag: 'fr_h'
Koorinaten (x+breite/y): 143+40/134
Sichtbarkeit: 0 (=normal)


Hier mein komplettes selbst erzeugtes Layout-Protokoll:
HTML:
class android.widget.TableRow 'null'   = 0+480/76   : 0
class android.widget.TextView 'hd_'   = 0+480/76   : 0
class android.widget.TableRow 'tr_h'   = 0+480/117   : 0
1--class android.widget.TextView     'lb_h'   = 0+50/134   : 0
1--class android.widget.EditText     'h'      = 50+93/117   : 0
1--class android.widget.TextView     'eh_h'   = 143+40/134   : 0
1--class android.widget.FrameLayout  'fr_h'   = 0+0/117   : 0
1--2--class at.TyE.MyLib.SmartBar    'sb_h'   = 0+0/117   : 0
1--2--class android.widget.ImageView 'pf_h'   = 0+0/117   : 0
class android.widget.TableRow 'tr_t'   = 0+480/189   : 0
1--class android.widget.TextView     'lb_t'   = 0+50/206   : 0
1--class android.widget.EditText     't'      = 50+93/189   : 0
1--class android.widget.TextView     'eh_t'   = 143+40/206   : 0
1--class android.widget.FrameLayout  'fr_t'   = 0+0/189   : 0
1--2--class at.TyE.MyLib.SmartBar    'sb_t'   = 0+0/189   : 0
1--2--class android.widget.ImageView 'pf_t'   = 0+0/189   : 0
class android.widget.TableRow 'img_formel'   = 0+480/261   : 0
class android.widget.ImageView 'null'   = 0+128/261   : 0
 

schlingel

Gesperrter Benutzer
Was genau möchtest du denn überhaupt erreichen? Mal mal ein kleines Bild in Paint oder so und ich schreib mal auf wie ich das machen würde.
 

AquaBall

Top Contributor
An deiner Stelle würde ich mir noch einmal genau überlegen was du machen möchtest und noch einmal beim Aufbau deiner View-Hierarchie anfangen.

Das wirkt zurzeit wirr und vor allem nicht auf verschiedene Breiten angepasst.

Du hast recht, auf verschiedene Breite oder mehr Sprachen, ... Soweit bin ich noch nicht.

Aber der Aufbau ist meiner Meinung nach recht simpel,
und soll erst mal stabil laufen, bevor ich mich um andere Formate kümmere.

SmartBar.png


Diese Balken sollen neben den TextViews erscheinen.
 

schlingel

Gesperrter Benutzer
Das würde ich mit einem geschachtelten LinearLayout lösen.

[xml]
<LinearLayout
android:width="fill_parent"
android:height="wrap_content"
android:eek:rientation="vertical">
<LinearLayout
android:width="fill_parent"
android:height="wrap_content"
android:eek:rientation="horizontal">
<TextView
android:width="wrap_content"
android:height="wrap_content"
<!-- sonstige Properties für Höhe --> />
<EditText
android:width="40sp"
android:height="wrap_content"
<!-- sonstige Properties für Höhe-EditBox --> />
<TextView
android:width="wrap_content"
android:height="wrap_content"
android:text="m"
<!-- sonstige Properties für Höhe --> />
<dein.packagename.CustomView
android:width="fill_parent"
android:height="wrap_content"
<!-- sonstige properties -->
</LinearLayout>

<LinearLayout
android:width="fill_parent"
android:height="wrap_content"
android:eek:rientation="horizontal">
<TextView
android:width="wrap_content"
android:height="wrap_content"
<!-- sonstige Properties für Zeit --> />
<EditText
android:width="40sp"
android:height="wrap_content"
<!-- sonstige Properties für Zeit-EditBox --> />
<TextView
android:width="wrap_content"
android:height="wrap_content"
android:text="sec"
<!-- sonstige Properties für Zeit --> />
<dein.packagename.CustomView
android:width="fill_parent"
android:height="wrap_content"
<!-- sonstige properties -->
</LinearLayout>
</LinearLayout>
[/xml]

Darum kannst du dann noch eine ScrollView packen wenn du das brauchst. Alternativ, wenn die Activity noch viele andere Views enthalten soll, kann man das auch mit einem RelativeLayout abbilden, das ist etwas performanter.

Das XML ist ungetestet aber ich glaub du bekommst eine Idee wie's zu machen ist.
 

AquaBall

Top Contributor
Mein CustomView verlangt ein FrameLayout, weil ich darin 2 Bilder überlagere.
Aber das kann (sollte) ich ja auch intern selbst erzeugen.
(Darf eigentlich nicht sein, dass meine Activity sich nach dem CustomView richten muss.)

So gesehen hat mich dein Tipp "ich soll's nochmal durchschauen" sehr viel weitergebracht.

Kann ich aber erst abends testen.

PS: Ungetestetes XML: ich muss es so oder so dynamisch machen, weil es deutlich mehr Zeilen, und deutlich komplexer werden kann. Aber die Idee gefällt mir gut. Irgendwie hat sich das tablerow eh exotisch angefühlt.

(PPS: Aber trotzdem ist mir unerklärlich, warum mein FrameLayout ganz links klebt (statt nach dem TextView), und gar nicht angezeigt wird?!? Ich hoffe, dass sich das mit LinearLayout dann anders verhält.)
 

schlingel

Gesperrter Benutzer
Aber das kann (sollte) ich ja auch intern selbst erzeugen.
(Darf eigentlich nicht sein, dass meine Activity sich nach dem CustomView richten muss.)
Gut erkannt ;-)

Das FrameLayout ist sowieso ein sehr biestiges View mit dem es generell mehr Probleme als Lösungen gibt. Ich geh dem aus dem Weg so gut es geht und verwende es nur bei Tabs.
 

AquaBall

Top Contributor
Übrigens: der nächste Schritt (wenn's dann funktioniert), ist in der Tat andere Formate, Landscape, ... (und auch andere Sprachen) zu ermöglichen.

Kennst du da ein einfaches Einführungs-Tutorial?
Offensichtlich bist du ja wirklich Andorid-Profi.
 

schlingel

Gesperrter Benutzer
Google bemüht sich da eh sehr, ich würde für Lokalisierung dazu greifen.

Punkto Landscape-Change: Das ist absolut keine Hexerei. Es gibt zwei Möglichkeiten damit umzugehen: Entweder das System regelt das, dann wird beim Umschalten zwischen den Ausrichtungen immer onCreate ausgeführt und eine neue View erstellt. Wenn deine content-view damit zurecht kommt bist du schon fertig. (Allerdings böse wenn du asynchrone Tasks laufen hast, die da möglicherweise eine View updaten wollen die's nimmer gibt)
Deswegen gibt's auch die zweite Möglichkeit selbst damit um zu gehen, da gibt's auch von Google ein Tutorial (letzter Punkt) dazu.
 

AquaBall

Top Contributor
So, kurzes Feedback:
Ich habe jetzt testhalber einfach NUR alle TableRow gegen LinearLayout (horizontal) getauscht und schon funktioniert es!

(Jetzt kommt natürlich noch meine Bereinigung des internen FrameLayout, ... aber das hat nichts mit der Funktion zu tun.)

Als Erklärung, warum's nicht funktioniert hat, akzeptiere ich mal, dass TableRow ohne TableView nicht gedacht ist, (obwohl dieser Versuch ja auch nichts gebracht hatte).
Aber was soll's.

Vielen Dank für deine Hilfe(n).
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
P trotz invalidate() wird onDraw() nicht aufgerufen Android & Cross-Platform Mobile Apps 15
J Android Teile in onDraw() nur einmal zeichnen Android & Cross-Platform Mobile Apps 2
Z Canvas onDraw() Variablen muss in der Methode initialisiert werden Android & Cross-Platform Mobile Apps 1
B onDraw Canvas jede Millisekunde aufrufen? Android & Cross-Platform Mobile Apps 12
W Reward Ads AdMob wird nicht ausgeliefert. Android & Cross-Platform Mobile Apps 9
J Spinner wird nicht aktualisiert Android & Cross-Platform Mobile Apps 6
Naxon89 Duplicate class kotlin - und dies ohne das es angewendet wird Android & Cross-Platform Mobile Apps 1
ImageView wird nicht angezeigt Android & Cross-Platform Mobile Apps 4
W Bildschirm Nutzung Überwachen der App Nutzer ink. was angeklickt wird Android & Cross-Platform Mobile Apps 35
N XY-Plottet keine Daten obwohl Funktion ausgeführt wird Android & Cross-Platform Mobile Apps 4
K Null-Pointer-Exception in ListView - wird über Datenbank gefüllt Android & Cross-Platform Mobile Apps 1
R Android Do not disturb: Sound wird nicht abgespielt Android & Cross-Platform Mobile Apps 2
O Google Admob Ad wird nicht geladen und App stürzt ab Android & Cross-Platform Mobile Apps 1
M Paper DB wird in Android Studio nicht erkannt Android & Cross-Platform Mobile Apps 7
R Audio wird nur 1 Mal abgespielt Android & Cross-Platform Mobile Apps 2
A GraphView => X- und Y-Achse wird nicht angezeigt Android & Cross-Platform Mobile Apps 5
A jpg wird im Android Studio nicht akzeptiert Android & Cross-Platform Mobile Apps 3
Arif Android Radiobutton wird nicht deaktiviert Android & Cross-Platform Mobile Apps 1
Arif Android Canvas wird nicht gezeichnet? Android & Cross-Platform Mobile Apps 0
J Notification wird nicht angezeigt wenn App nicht offen ist. Android & Cross-Platform Mobile Apps 6
M TypedArray-Resource wird falsch geladen Android & Cross-Platform Mobile Apps 7
W Preview wird nicht korrekt angezeigt Android & Cross-Platform Mobile Apps 0
B Profilpic wird nach anmeldung nicht angezeigt. Android & Cross-Platform Mobile Apps 2
K Methode wird nicht gefunden Android & Cross-Platform Mobile Apps 1
J Kamera - Foto wird nicht gespeichert Android & Cross-Platform Mobile Apps 2
V Android Wird mein Vorhaben funktionieren? (Apk Datei decompilieren, bearbeiten, compilieren) Android & Cross-Platform Mobile Apps 2
G App wird nach Installation auf Smartphone beendet Android & Cross-Platform Mobile Apps 1
L Dialog anzeigen wenn auf Button gedrückt wird. Android & Cross-Platform Mobile Apps 4
S Android neue Version des Programms wird nicht in Emulator geladen Android & Cross-Platform Mobile Apps 1
O Android Switch Widget wird nicht angezeigt Android & Cross-Platform Mobile Apps 1
M Android ListView wird nicht dargestellt Android & Cross-Platform Mobile Apps 2
N PriceScannerApp: warum wird nach dem Scannen Display gleich schwarz? Android & Cross-Platform Mobile Apps 4
P Herausfinden, welches Fragment gerade angezeigt wird. Android & Cross-Platform Mobile Apps 1
M Android Nur erste Zeile wird vom Server empfangen Android & Cross-Platform Mobile Apps 0
A App wird bei start des Timers beendet Android & Cross-Platform Mobile Apps 1
A Wieso wird die App beendet ??? Android & Cross-Platform Mobile Apps 2
B Alle Daten gehen verloren, wenn die Displaysperre aktiviert wird? Android & Cross-Platform Mobile Apps 21
W XML Layout: wann wird geladen? Android & Cross-Platform Mobile Apps 10
K Android Temperaturconverter, R.id.element wird nicht gefunden Android & Cross-Platform Mobile Apps 20
A Android Dialog wird nicht sofort angezeigt Android & Cross-Platform Mobile Apps 12
W ImageView wird nicht angezeigt Android & Cross-Platform Mobile Apps 19
T Android: ListView-Adapter: Adapter wird ständig aufgerufen Android & Cross-Platform Mobile Apps 2
F Android Datenbank upgrade wird nicht durchgeführt Android & Cross-Platform Mobile Apps 2
F Android R.raw wird nicht gefunden Android & Cross-Platform Mobile Apps 5
P ID wird nicht erzeugt Android & Cross-Platform Mobile Apps 2
C Problem Device/Emulator wird nicht erkannt Android & Cross-Platform Mobile Apps 3
R Zeichen-Codierung in (SMS) TextMessage, "_" wird § Android & Cross-Platform Mobile Apps 2
W MoveEvent reagiert nicht Game Android & Cross-Platform Mobile Apps 1
W Cookie Manger übermittelt nicht Android & Cross-Platform Mobile Apps 1
M PythonInterpreter funktioniert nicht richtig NoClassDefFoundError Android & Cross-Platform Mobile Apps 1
I Android SharedPreferences wenn App auf externem Speicher klappt nicht Android & Cross-Platform Mobile Apps 0
W Frisch generiertes Projekt Value nicht findbar NavController Drawer Android & Cross-Platform Mobile Apps 12
W WebView Linkvertise lädt nicht richtig oder bleibt grau Android & Cross-Platform Mobile Apps 10
W AlertDialog Eigenschaften existiert nicht mehr. Android & Cross-Platform Mobile Apps 14
W Wieso gehen Log nicht im Service? Android & Cross-Platform Mobile Apps 20
I Android ListView (Custom) soll auf Hardwaretasten nicht reagieren. Android & Cross-Platform Mobile Apps 10
CT9288 Zufällige Zahlengenerierung scheint nicht richtig zu funktionieren Android & Cross-Platform Mobile Apps 5
M App crasht und ich bekomme es nicht behoben.:( Android & Cross-Platform Mobile Apps 17
K Android Android In-App-Purchase lädt nicht Android & Cross-Platform Mobile Apps 0
Besset Android http request an interne ip adresse funktioniert nicht Android & Cross-Platform Mobile Apps 8
R Android Visualizer engine kann nicht initialisiert werden Android & Cross-Platform Mobile Apps 3
OSchriever Navigation drawer Strings ändern sich nicht Android & Cross-Platform Mobile Apps 0
W Code läuft unter SDK 27 aber nicht SDK 30 Android & Cross-Platform Mobile Apps 17
N Android Ich kann mit meiner App nicht auf die Datenbank zugreifen Android & Cross-Platform Mobile Apps 4
ruutaiokwu Android Selbst entwickelter SMTP-Client läuft auf PC, nicht aber auf Android Android & Cross-Platform Mobile Apps 9
W Android Wieso geht getApplicationContext() bei Toast, aber nicht bei AlertDialog.Builder? Android & Cross-Platform Mobile Apps 36
A Android Studio: while-Schleife beginnt nicht Android & Cross-Platform Mobile Apps 5
T Android SDK-Manager startet nicht in Eclipse Android & Cross-Platform Mobile Apps 5
J Download und speichern mit jsch klappt nicht Android & Cross-Platform Mobile Apps 5
J Service starte nicht mehr Android & Cross-Platform Mobile Apps 13
J Firebase und Emulator startet nicht Android & Cross-Platform Mobile Apps 2
L ListView aktuallisiert sich nicht Android & Cross-Platform Mobile Apps 15
J ArrayAdapter zeigt Liste nicht an Android & Cross-Platform Mobile Apps 0
L Android Animationen werden nicht angezeigt Android & Cross-Platform Mobile Apps 0
Excess Android Service läuft nicht in Sandby weiter Android & Cross-Platform Mobile Apps 2
J App funktioniert auf Android 5, auf 6 nicht Android & Cross-Platform Mobile Apps 2
K Log.v geht nicht Android & Cross-Platform Mobile Apps 4
L Android Android Studio - Exportierte APK funktioniert nicht Android & Cross-Platform Mobile Apps 6
L Android Methode funktioniert nicht unter Android Android & Cross-Platform Mobile Apps 3
L Android Java scheint XML nicht zu finden Android & Cross-Platform Mobile Apps 11
J android Spinner funktioniert nicht Android & Cross-Platform Mobile Apps 14
B Android osmdroid möchte nicht das es mapnik herunterlädt Android & Cross-Platform Mobile Apps 2
apple_pie1998 SharedPreferences funktionieren nicht... Android & Cross-Platform Mobile Apps 17
B Android Textdatei laden (klappt nicht) Android & Cross-Platform Mobile Apps 4
S Android null pointer (ich verstehs nicht) Android & Cross-Platform Mobile Apps 2
S Android Emulator startet nicht vollständig. Android & Cross-Platform Mobile Apps 0
S Neue Activity lässt sich nicht starten Android & Cross-Platform Mobile Apps 28
M Notification nicht mehr senden nachdem sie geklickt wurde Android & Cross-Platform Mobile Apps 0
D Android Tabs nutzen - PagerTitleStrip haut nicht hin Android & Cross-Platform Mobile Apps 4
K MediaPlayer Soundklasse Start und Stop (stop funktioniert nicht) Android & Cross-Platform Mobile Apps 1
O Android Anfänger: Quellcode nicht gefunden Android & Cross-Platform Mobile Apps 3
B Timer geht nicht Android & Cross-Platform Mobile Apps 2
S Umlaute werden trotz UTF-8 nicht angezeigt? Android & Cross-Platform Mobile Apps 6
S SPLIT funktion bei STRING funktioniert nicht! Android & Cross-Platform Mobile Apps 4
J Android Breaking Point in Eclipse hält nicht an? Android & Cross-Platform Mobile Apps 5
J Android SimpleDateFormat parser funktioniert nicht richtig? Android & Cross-Platform Mobile Apps 4
L Android Apache POI: Datei speichern geht nicht Android & Cross-Platform Mobile Apps 1
Y Erstes kleines Android Programm will nicht so recht... Android & Cross-Platform Mobile Apps 1
S Foto-app hält galerie nicht aktuell Android & Cross-Platform Mobile Apps 9
D Man sieht nicht ob Button gedrückt wurde! Android & Cross-Platform Mobile Apps 10

Ähnliche Java Themen

Neue Themen


Oben