XML Layout: wann wird geladen?

Warrior7777

Bekanntes Mitglied
Hallo miteinander!

Entschuldigung für den spartanischen Titel, ich hoffe, man versteht trotzdem, was ich meine... :oops: Nun die ausformulierte Frage: Werden XML-Layouts beim Starten der App geladen oder zu einem anderen Zeitpunkt? Also: wenn ich eine Root ViewGroup mit findViewById() lade, erreiche ich mit getViewById() eine Referenz auf die bereits instanzierten Child Views?
 

schlingel

Gesperrter Benutzer
Werden XML-Layouts beim Starten der App geladen oder zu einem anderen Zeitpunkt?
Kommt drauf an. Du kannst auf 3 verschiedene Arten (die mir bekannt sind) Views bzw. ViewGroups laden:
1. setContentView in der Activity
2. per LayoutInflater#inflate
3. per Java-Code instanzieren.

Sobald die ViewGroup mit Option 1 oder 2 ins Leben gerufen wurde, existieren auch ihre Kinder sofern sie im XML definiert sind.

findViewById lädt nichts sondern sucht dir nur die Referenz aus dem bereits bestehenden View-Baum heraus.
 

Warrior7777

Bekanntes Mitglied
Ok danke genau das hatte ich gemeint! :)

Also:
Java:
RelativeLayout root=(RelativeLayout) getLayoutInflater().inflate(R.layout.main);
View child=findViewById(R.id.child);
Dann ist jetzt in child eine Referenz auf das mit inflate() instanzierte Kind-Objekt vorhanden, oder?

Leider hab ich moch ein Problem. Ich habe eine Activity, die eine ListView anzeigt. Bei setAdapter() erscheint eine Fehlermeldung:
java.lang.RuntimeException: Unable to start activity ComponentInfo{[appname]/[activityname]}: java.lang.NullPointerException

Nun, der Adapter wurde durch Übergabe einer List an den Konstruktor instanziert. Ich hab das mit Breakpoint geprüft, der Adapter ist nicht leer. Daher habe ich einen anderen Verdacht. Könnte es evt. an der angegebenen Layout-Datei liegen? Hier ist sie:

[XML]<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>[/XML]

Hier noch der Adapter-Konstruktor:
Java:
adapter=new ArrayAdapter<SensorWrapper>(this, R.layout.textview, list);

Ich hoffe ich hab genug Infos gegeben und Ihr könnt mir helfen! :)

Noch einen schönen Abend!
 

schlingel

Gesperrter Benutzer
Zeig mal den Code von der ganzen getView, vom Konstruktor des Adapters, der Stelle wo du ihn instanzierst und der Stelle wo du ihn per setAdapter der ListView zuweist.
 

Warrior7777

Bekanntes Mitglied
Ok hier der ganze Code der Activity bis zum Fehler:
Java:
package ch.renatobellotti.firstapp;

//imports
//...

public class StartActivity extends Activity implements OnItemClickListener{
    
	private ArrayAdapter<SensorWrapper> adapter;
	private ListView listView;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //set the ListView
        Log.d(TAG, "test");
        setContentView(R.layout.main);
        
        Log.d(TAG, "Test");
        
        //get a list of all sensors
        SensorManager manager=(SensorManager) getSystemService(SENSOR_SERVICE);
        List<Sensor> sensors=manager.getSensorList(Sensor.TYPE_ALL);
        
        //wrap the sensors
        List<SensorWrapper> list=new LinkedList<SensorWrapper>();
        for(Sensor s: sensors){
        	list.add(new SensorWrapper(s));
        }
        
        //get the adapter
        adapter=new ArrayAdapter<SensorWrapper>(this, R.layout.textview, list);

        //set the adapter
        Log.d(TAG, "vor listView.setAdapter()");
        listView.setAdapter(adapter);
        Log.d(TAG, "nach listView.setAdapter()");
        
        listView.setOnItemClickListener(this);
    }
}
Die letzte Ausgabe vor der Fehlermeldung ist "vor listView.setAdapter()". Danach kommen Fehlermeldungen. Die erste habe ich oben gepostet, die anderen ergeben sich wahrscheinlich als Folge.

textview.xml hab ich auch oben gepostet. Hier ist noch main.xml:
[XML]<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:eek:rientation="vertical" />[/XML]

Mir ist noch ein anderer Gedanke gekommen: Brauche ich für Sensoren eine Permission?

Wenn etwas fehlt, sagt es mir bitte! Ich hoffe Ihr wisst, was ich falsch mache. :)

Noch ein schönes Wochenende!
 

schlingel

Gesperrter Benutzer
Du holst dir nirgends die Referenz auf die Listview. Deswegen ist die auch null und deswegen bekommst du eine NPE.

Die wurde zwar geladen aber du hast keine Referenz darauf um damit arbeiten zu können.

Da fehlt noch das:

listView = (ListView)findViewById(R.id.deineListview);
 

Warrior7777

Bekanntes Mitglied
Natürlich! Dass ich das nicht selbst gesehen habe... :oops:
Vielen Dank! Genau wegen solchen Dingen sollte man mit kleinen Probe-Apps anfangen. Dabei lernt man die Basics. Bei grösseren Programmen sollte man sich dann nicht auch noch um die Basics kümmern müssen. Nochmals vielen Dank, jetzt geht es!
 

Warrior7777

Bekanntes Mitglied
Hab leider noch ein Problem... Ich denke mal, das geht in dasselbe hinein; ich seh aber nicht, wo der Hund begraben liegt. :(

Java:
    	LayoutInflater inflater=getLayoutInflater();
    	RelativeLayout result=(RelativeLayout) inflater.inflate(R.layout.sensorinfoscreen, null);
    	TextView namecontent=(TextView) findViewById(R.id.namecontent);

Das Problem: result ist ok, aber namecontent ist ==null. woran könnte das liegen?:bahnhof: Die Child-Views sind nicht null, das hab ich mit Breakpoints rausgefunden. Nützlich, diese Haltpunkte! ;)

Ich hoffe Ihr könnt mir helfen! :)
 

Warrior7777

Bekanntes Mitglied
[XML]<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootlayout"
android_layout_height="fill_parent"
android:layout_width="fill_parent">

<!-- the name -->
<TextView
android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/sensor_name" />

<TextView
android:id="@+id/namecontent"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toRightOf="@id/name"
android:text="@string/default_name" />

<!-- the version -->
<TextView
android:id="@+id/version"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/name"
android:text="@string/sensor_version" />

<TextView
android:id="@+id/versioncontent"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toRightOf="@id/version" />

<!-- the vendor -->
<TextView
android:id="@+id/vendor"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/version"
android:text="@string/sensor_vendor" />

<TextView
android:id="@+id/vendorcontent"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toRightOf="@id/vendor" />

<!-- the type -->
<TextView
android:id="@+id/type"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/vendor"
android:text="@string/sensor_type" />

<TextView
android:id="@+id/typecontent"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toRightOf="@id/type" />

<!-- the battery power -->
<TextView
android:id="@+id/power"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/type"
android:text="@string/sensor_power" />

<TextView
android:id="@+id/powercontent"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toRightOf="@id/power" />

<!-- the resolution -->
<TextView
android:id="@+id/resolution"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/power"
android:text="@string/sensor_resolution" />

<TextView
android:id="@+id/resolutioncontent"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toRightOf="@id/resolution" />

<!-- the maximum range -->
<TextView
android:id="@+id/range"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/resolution"
android:text="@string/sensor_range" />

<TextView
android:id="@+id/rangecontent"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toRightOf="@id/range" />

<!-- the minimum delay -->
<TextView
android:id="@+id/delay"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/range"
android:text="@string/sensor_delay" />

<TextView
android:id="@+id/delaycontent"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toRightOf="@id/delay" />

<!-- the button to return to the sensor overview -->
<Button
android:id="@+id/back"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/delay"
android:text="@string/back_text" />
</RelativeLayout>[/XML]
Danke für Dein Interesse und Deine Hilfsbereitschaft! :)
 

schlingel

Gesperrter Benutzer
Java:
TextView namecontent=(TextView) findViewById(R.id.namecontent);

Ich weiß nicht genau wo das aufgerufen wird aber es sieht danach aus, als würde es innerhalb einer Activity aufgerufen. Wenn du in einer Activity findViewById aufrufst, bekommst du die View von der aktuellen Content-View zurück.

Du möchtest aber eine View aus der result-ViewGroup haben. Also musst du das so aufrufen:

Java:
TextView namecontent=(TextView) [B]result[/B].findViewById(R.id.namecontent);
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
W Ausklappbares Verlängertes XML Layout Android & Cross-Platform Mobile Apps 6
W Android Wieso kann ich keine ListView mehr zum Layout hinzufügen? Android & Cross-Platform Mobile Apps 1
A Android-Studio: 2. Layout nach kurzer Zeit aufzeigen Android & Cross-Platform Mobile Apps 2
S Android Layout - welchen Typ? Android & Cross-Platform Mobile Apps 3
B Android Multiple Screens- layout-sw600dp Android & Cross-Platform Mobile Apps 1
D Android Layout für alle Geräte Android & Cross-Platform Mobile Apps 4
W Android Designfrage / Layout / Activity / Fragments Android & Cross-Platform Mobile Apps 2
B Layout Bibliothek Android & Cross-Platform Mobile Apps 8
B Android Spiele-Layout umsetzen Android & Cross-Platform Mobile Apps 5
O zurück Schaltfläche in voriges Layout Android & Cross-Platform Mobile Apps 5
B Erste Android-App: setContentView(R.layout.main) funktioniert nicht Android & Cross-Platform Mobile Apps 6
D Android Viele Buttons und ein Layout Android & Cross-Platform Mobile Apps 6
M Android Game, welche Layout? Android & Cross-Platform Mobile Apps 2
B Eigene View xml-Layout einbinden Android & Cross-Platform Mobile Apps 1
R Android Layout Bild mit Text Android & Cross-Platform Mobile Apps 13
K Rand bei (Table,Relative,Linear)Layout - wie bekomme ich ihn weg? Android & Cross-Platform Mobile Apps 3
D Android Layout Problem Android & Cross-Platform Mobile Apps 2
P Android Nach Animation Layout auf alten Platz Android & Cross-Platform Mobile Apps 3
T Android Layout Update Animation Android & Cross-Platform Mobile Apps 3
P Probleme mit xml-Layout Android & Cross-Platform Mobile Apps 2
F Layout mit listViews (Scrolling-Probleme) Android & Cross-Platform Mobile Apps 2
G Fehlermeldung: "No XML content. Please add a root view or layout to your documet." Android & Cross-Platform Mobile Apps 7
N Android xml - graphical layout nur noch weiß :o Android & Cross-Platform Mobile Apps 4
T Android Merkwürdigkeiten im Layout Android & Cross-Platform Mobile Apps 7
JAVAnnik Android Layout ändern in Thread Android & Cross-Platform Mobile Apps 2
A Android Browser öffnen, XML-GUI-Layout Android & Cross-Platform Mobile Apps 23
A Absolute Layout soll auf jedem Gerät gleich aussehen Android & Cross-Platform Mobile Apps 4
S Android Layout Problem mit fill_parent Android & Cross-Platform Mobile Apps 5
tfa Android Layout-Probleme: View programmatisch erweitern (addContentView) Android & Cross-Platform Mobile Apps 7
M height, layout_height, width und layout_width - Wann brauche ich was warum? Android & Cross-Platform Mobile Apps 1
J Android Wann ist eine App privat? Android & Cross-Platform Mobile Apps 10
H Android Android Studio mit NDK ünterstützung, wann? Android & Cross-Platform Mobile Apps 1
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
P trotz invalidate() wird onDraw() nicht aufgerufen Android & Cross-Platform Mobile Apps 15
K Android Temperaturconverter, R.id.element wird nicht gefunden Android & Cross-Platform Mobile Apps 20
A onDraw wird nicht aufgerufen Android & Cross-Platform Mobile Apps 14
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

Ähnliche Java Themen

Neue Themen


Oben