Android Problem mit ListView und OnItemClickListener..

android_domi

Mitglied
Hallo,

ich bin gerade dabei eine kleine App zu machen.

Ich habe eine Liste erstellt und habe darin 6 Einträge, wenn ich auf einen eintrag klicke soll sich mit "setContenView"
die nächste Seite öffnen und im Textfeld auf dieser seite ein kleiner Text stehen.

Mein Problem ist jetzt, dass ich wenn ich die App (in Eclipse) ausführe und auf einen Listeneintrag klicke das sich die App schließt. Ich habe mir schon mehrere Tutorials angesehen und gelesen aber leider keine antwort gefunden.. :rtfm:

Hier der Quellcode von der MainActivity.java:
Java:
        final ListView lview;
    	final TextView tview;
    	final String[] list_value = {"bsp","bsp2","bsp3","bsp4","bsp5","bsp6"};


                    lview = (ListView) findViewById(R.id.lv1 );
                    tview = (TextView) findViewById(R.id.auswahl_geb_text);
                    
                    ArrayAdapter<String> array_adapter = new ArrayAdapter<String>(MainActivity.this,            android.R.layout.simple_list_item_1, list_value) ;
        			lview.setAdapter(array_adapter);
        			lview.setOnItemClickListener (new OnItemClickListener(){

						@Override
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							
							switch(lview.getPositionForView(view)){
							
								case 0:{
								     tview.setText("Es liegen folgende Aufträge für bsp vor: ");
								    setContentView(R.layout.auswahl_geb);
									 break;
								}
								 case 1:{
								    tview.setText("Es liegen folgende Aufträge für bsp2 vor: ");
									 setContentView(R.layout.auswahl_geb);
									 break;
								}
								 case 2:{
								     tview.setText("Es liegen folgende Aufträge für bsp3 vor: ");
 									setContentView(R.layout.auswahl_geb);
									 break;
								 }
								case 3:{
								    tview.setText("Es liegen folgende Aufträge für bsp4 vor: ");
									setContentView(R.layout.auswahl_geb);
									break;
								}
								case 4:{
								    tview.setText("Es liegen folgende Aufträge für bsp5 vor: ");
									setContentView(R.layout.auswahl_geb);
									break;
								}
								case 5:{
								    tview.setText("Es liegen folgende Aufträge für die bsp6 vor: ");
									setContentView(R.layout.auswahl_geb);
									break;
								}
																							
							}
        				
						}
					});

Hier der Code meiner angemeldet.xml:

Java:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/angemeldet_text_1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ListView
        android:id="@+id/lv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>
    
</LinearLayout>

Und hier mein Code für die auswahl_geb.xml:

Java:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    
   <TextView
	android:id="@+id/auswahl_geb_text"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="@string/auswahl_geb_text_1"
	android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Ich hoffe ihr versteht mein Prolem und das ihr mir helfen könnt :)
Danke schon mal im Vorraus
 

dzim

Top Contributor
Ein paar Bemerkungen vorweg: Sosehr ich auch noch nicht mit Android Studio warm werde, empfehle ich dir dorthinzuwechseln. Es ist leider die offizielle Android-IDE. Und man kann die Keybindings auf "Eclipse"-Schema umstellen, so dass man sich nicht völlig fremd vorkommt.
Es sei denn, du verwendest schon den neuen Eclipse-Fork des ADT-Plugins (Andmore -> Ecllipse Projekt)... Dann vergiss es :)

Das zweite ist: Ich bin mir ziemlich sicher, dass der LogCat-View dir einen StackTrace gibt, wenn du die Anwendung auch von Eclipse aus startest. Dieser wäre interessant und hilfreich gewesen.

Als drittes aber vermute ich, dass es dein mehrfacher Call auf #setContentView ist. Was soll der bezwecken? Den Inhalt (die UI) der Activity auf der du bist neu laden? Für so etwas nutzt man Fragmente und tauscht so Teile auf der Activity aus (diese Teile können natürlich auch "full-screen" sein).

Bitte erkläre also noch einmal, was der #setContentView-Call bezwecken sollte.

Grüsse,
Daniel
 

android_domi

Mitglied
Erstmal Danke für deine schnelle Rückmeldung.

Hier ist die LogCat meldung:



04-22 07:17:51.267: D/dalvikvm(617): GC_FOR_ALLOC freed 127K, 2% free 11027K/11207K, paused 35ms, total 37ms
04-22 07:17:51.277: I/dalvikvm-heap(617): Grow heap (frag case) to 12.155MB for 1422024-byte allocation
04-22 07:17:51.337: D/dalvikvm(617): GC_CONCURRENT freed 1K, 2% free 12415K/12615K, paused 27ms+3ms, total 66ms
04-22 07:17:51.656: D/gralloc_goldfish(617): Emulator without GPU emulation detected.
04-22 07:18:26.546: D/AndroidRuntime(617): Shutting down VM
04-22 07:18:26.546: W/dalvikvm(617): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-22 07:18:26.596: E/AndroidRuntime(617): FATAL EXCEPTION: main
04-22 07:18:26.596: E/AndroidRuntime(617): java.lang.NullPointerException
04-22 07:18:26.596: E/AndroidRuntime(617): at com.example.digitalecheckliste.MainActivity$1.onItemClick(MainActivity.java:122)
04-22 07:18:26.596: E/AndroidRuntime(617): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
04-22 07:18:26.596: E/AndroidRuntime(617): at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
04-22 07:18:26.596: E/AndroidRuntime(617): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2859)
04-22 07:18:26.596: E/AndroidRuntime(617): at android.widget.AbsListView$1.run(AbsListView.java:3533)
04-22 07:18:26.596: E/AndroidRuntime(617): at android.os.Handler.handleCallback(Handler.java:615)
04-22 07:18:26.596: E/AndroidRuntime(617): at android.os.Handler.dispatchMessage(Handler.java:92)
04-22 07:18:26.596: E/AndroidRuntime(617): at android.os.Looper.loop(Looper.java:137)
04-22 07:18:26.596: E/AndroidRuntime(617): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-22 07:18:26.596: E/AndroidRuntime(617): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 07:18:26.596: E/AndroidRuntime(617): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 07:18:26.596: E/AndroidRuntime(617): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-22 07:18:26.596: E/AndroidRuntime(617): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-22 07:18:26.596: E/AndroidRuntime(617): at dalvik.system.NativeStart.main(Native Method)


Mein #setContentView soll bezwecken, dass uch auf die nächste seite komme. Ich befinde mich auf der angemeldet.xml und möchte auf auswahl_geb.xml.
Es soll funktionieren wie eine Ordner Struktur mit unterordnern. Es sind 3 Seiten (Ich momnetan noch 2 aber es sollen 3 werden) und auf allen sind Listen und wenn man auf einen Listen Eintrag klickt soll man auf der entersprechenden Seite landen.
Also aus Seite 1 ist eine Liste wo man etwas aufwählen soll, dann kommt man auf eine Seite wo wieder ein Liste ist und man auwählen muss und das ganze nochmal.

Ich hoffe das ich mich verständlich ausgedrückt habe..

Viele Grüße
Dominic
 

Ch4t4r

Aktives Mitglied
Wenn du jedesmal die UI neusetzt, wirst du die Zurücktaste (ohne weitere Arbeit) nicht benutzen können. In so einem Fall arbeitet man mit mehreren activities.

Solltest du das nicht wollen kommt der Fehler wahrscheinlich daher, dass die UI nicht asynchron geupdated werden kann (und ich glaube der listener ist asynchron). Es muss im UI-thread laufen (runOnUIThread())
 

android_domi

Mitglied
Wie kann ich das denn machen das die Zurücktasten ohne weiterenaufwand Funktionieren?
Ich habe noch nie mit mehreren Activities gearbeitet, kannst du mir das vlt. ein kurzes Bsp. sagen/zeigen?

Was ist eine UL?

Es Funktioniert jetzt soweit, dass ich auf die nächste Seite Komme.
Ich habe das setTExt() wegelassen und daran lag es, jetzt weiß ich jedoch nicht wie ich den Text auf die nächste seite bekomme.
Ich glaube es gibt sowas wie dynamische Strings nicht oder?
Kann ich das mit Preferences machen?
 

Ch4t4r

Aktives Mitglied
Ui heißt User Interface.

Benutzen kannst du sie schon, du wirst aber wenn du z.b in der dritten Liste bist
nicht in die zweite zurückkommen.
Ich denke, dass du dir wegen activities tutorials im Internet angucken solltest, das sind Grundlagen und es gibt genug.
 

android_domi

Mitglied
Und noch ein Frage: Müsste ich dann nicht in meinem Fall bei 5 Listen einträgen, auch 5 Activities erstellen damit jeder Listen eintrag zu einer anderen Seite/Liste führt?
 

Ch4t4r

Aktives Mitglied
Eine activity kannst du dir wie ein Fenster vorstellen. Rufst du ein neues Fenster auf schiebt das alte sich in den Hintergrund. Activities sind das grundlegendenste in Android, aber wie gesagt, bei so vielen Anleitungen im internet findest du schon was informatives.
 

Maggot

Bekanntes Mitglied
Hallo android_domi,

ich denke dieses Tutorial wird dir weiter helfen. Starting Another Activity | Android Developers
Du hast 2 Activities: 1ne mit deiner ListView und eine andere was dann den Inhalt anzeigt. Wenn du jetzt auf ein ListView Item klickst öffnest du einfach die 2. Activity und übergibst ihr mit intent.putExtra... die benötigten Daten (in deinem Fall Text).

Das ist nicht die eleganteste Lösung aber besser als nichts :)

Lg
 

dzim

Top Contributor
BTW: Dein Fehler steht mitten in dem LogCat-Einträgen:

04-22 07:18:26.596: E/AndroidRuntime(617): java.lang.NullPointerException
04-22 07:18:26.596: E/AndroidRuntime(617): at com.example.digitalecheckliste.MainActivity$1.onItemClick(MainActivity.java:122)

In Zeile 122 deiner Klasse versuchst du, auf ein noch nicht initialisiertes Objekt (eine Null-Referenz) zuzugreifen. Ohne die genaue Stelle zu kennen, ist es schwierig zu sagen, was genau schief geht.

Wenn du keine Activities verwenden möchtest, kannst du auch Fragmente nutzen (inklusive Back-Button), dabei wird lediglich der Inhalt der Activity (wie @Ch4t4r schon bemerkte: das ist quasi ein Full-Screen-Fenster in Android) ausgetauscht. Ist IMHO etwas einfacher Daten über deren Grenzen auszutauschen, als bei Activities.

Fragments | Android Developers

Unter Android wirst du aber nicht drum herum kommen, dich sowohl mit Activities, als auch Fragmenten auseinanderzusetzen. Und auch mit einem ordentlichen und flexiblen Layout deiner UIs...
Einfach dran bleiben: Jeder hat da mal als Frischling begonnen!
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
W Prüfen, ob App auf Gerät installiert ist Problem S10 Android & Cross-Platform Mobile Apps 11
W In App Purchase Problem? Android & Cross-Platform Mobile Apps 36
W Problem mit Android Studio Android & Cross-Platform Mobile Apps 0
T Android R.string.test+i Problem Android & Cross-Platform Mobile Apps 2
K Android to Pi | Websocket Problem Android & Cross-Platform Mobile Apps 3
N Intent und finish() Problem Android & Cross-Platform Mobile Apps 5
B Android App Programmierung Einsteiger Problem Android & Cross-Platform Mobile Apps 4
emeraldo Android Problem mit Bottomnavmenu Android & Cross-Platform Mobile Apps 10
I Das Problem mit der Tastatur... android:windowSoftInputMode="adjustPan" Android & Cross-Platform Mobile Apps 1
M Android App → Problem mit dem Speichern von einem Bitmap–Objekt. Android & Cross-Platform Mobile Apps 1
A Android Android Studio Emulator Problem Android & Cross-Platform Mobile Apps 1
S Android Studio Bluetooth App Problem Android & Cross-Platform Mobile Apps 6
J TicTacToe Problem bei kontrolle Android & Cross-Platform Mobile Apps 7
J Button array ID Problem Android & Cross-Platform Mobile Apps 2
M Problem bei Werteübergabe, MSQL verbindung Android & Cross-Platform Mobile Apps 3
S Android Problem mit Android Virtual Device erstellung. Android & Cross-Platform Mobile Apps 2
Anfänger2011 Text to Speech Problem Android & Cross-Platform Mobile Apps 1
S Android Android java onclick listener Problem Android & Cross-Platform Mobile Apps 9
K Problem mit arraylist und button Android & Cross-Platform Mobile Apps 16
R W-Lan Problem über Sockets Android & Cross-Platform Mobile Apps 1
P ViewPager Problem Android & Cross-Platform Mobile Apps 1
A Android Problem mit Video von Youtube abspielen Android & Cross-Platform Mobile Apps 4
A Android Problem mit Zurücktaste und ausgabe der Aktuellen Seite Android & Cross-Platform Mobile Apps 6
B Android Problem mit Soundwiedergabe Android & Cross-Platform Mobile Apps 2
T Android Android Sensor: Java Problem Android & Cross-Platform Mobile Apps 1
G Problem beim Rendern von 3D-Objekt Android & Cross-Platform Mobile Apps 0
L Android Gyroscope Sensor Problem Android & Cross-Platform Mobile Apps 2
S Android GPS Problem Android & Cross-Platform Mobile Apps 24
J Eclipse Emulator Problem Android & Cross-Platform Mobile Apps 1
J Eclipse Emulator Problem Android & Cross-Platform Mobile Apps 0
B Android Problem mit Rückgabewert Android & Cross-Platform Mobile Apps 13
L Android komisches Bitmap-Größe-Problem Android & Cross-Platform Mobile Apps 8
D Android Layout Problem Android & Cross-Platform Mobile Apps 2
R Problem mit View in ScrollView Android & Cross-Platform Mobile Apps 6
R Eclipse + AndroidSDK - Problem mit Referenzen Android & Cross-Platform Mobile Apps 6
M Problem mit setOnClickListener Android & Cross-Platform Mobile Apps 4
DaniSahne96 Problem beim Appdebuggen auf Smartphone Android & Cross-Platform Mobile Apps 3
P Android Problem beim Widget - Denkfehler ? Android & Cross-Platform Mobile Apps 2
M GCM IntentService Problem Android & Cross-Platform Mobile Apps 3
D Android Gallery Problem Android & Cross-Platform Mobile Apps 5
P Problem mit Cell id Android & Cross-Platform Mobile Apps 6
L Android Problem mit "spinner" Android & Cross-Platform Mobile Apps 10
D Android problem mit geschwindigkeitsberechnung app Android & Cross-Platform Mobile Apps 2
E Android Problem mit Contact Provider Android & Cross-Platform Mobile Apps 1
H Android Problem mit ListActivity Android & Cross-Platform Mobile Apps 3
S Android Layout Problem mit fill_parent Android & Cross-Platform Mobile Apps 5
F Android ExpandableList, SimpleCursorTreeAdapter, Cursor Problem Android & Cross-Platform Mobile Apps 2
A Android Problem mit Long.getLong() bzw. Integer.getInteger() Android & Cross-Platform Mobile Apps 2
A Problem mit HTTP- Verbindung Android & Cross-Platform Mobile Apps 4
V [Java] und [JavaME] ClientServer StreamConnection . Problem beim lesen / schreiben Android & Cross-Platform Mobile Apps 2
F Eclipse JAD File erzeugen -- Problem Android & Cross-Platform Mobile Apps 10
R Ein Problem beim ausführen von folgendem Quelltext Android & Cross-Platform Mobile Apps 11
M Problem mit dem Auslesen von System Properties Android & Cross-Platform Mobile Apps 7
P wtk problem Android & Cross-Platform Mobile Apps 3
G Math exp() Problem Android & Cross-Platform Mobile Apps 4
G S40 Problem Android & Cross-Platform Mobile Apps 8
A Problem beim Subtrahieren eines Double von einem Double Android & Cross-Platform Mobile Apps 5
C Problem Device/Emulator wird nicht erkannt Android & Cross-Platform Mobile Apps 3
S Image Problem Android & Cross-Platform Mobile Apps 11
M Problem mit den Softkeys Android & Cross-Platform Mobile Apps 4
G J2ME jar-problem Android & Cross-Platform Mobile Apps 10
S Komisches Problem Android & Cross-Platform Mobile Apps 3
F Problem beim Erstellen der Jar File Android & Cross-Platform Mobile Apps 4
A Problem: Canvas-Grösse Motorola RAZR v3r Android & Cross-Platform Mobile Apps 8
S Problem mit Einbindung einer externer Bibliothek Android & Cross-Platform Mobile Apps 2
G Random - Problem Android & Cross-Platform Mobile Apps 5
E problem mit den resourcen Android & Cross-Platform Mobile Apps 2
O Problem mit Datagramconnection Android & Cross-Platform Mobile Apps 2
P Problem mit der Uhrzeit Android & Cross-Platform Mobile Apps 2
S Problem auf dem Handy Android & Cross-Platform Mobile Apps 3
J ListView Item individuell einfärben Android & Cross-Platform Mobile Apps 17
I Android ListView, Werte aktualisieren ohne die Liste komplett neu zu laden Android & Cross-Platform Mobile Apps 5
W ListView OnItemClicklistener setzen mit Ausgabe Android & Cross-Platform Mobile Apps 35
K Null-Pointer-Exception in ListView - wird über Datenbank gefüllt Android & Cross-Platform Mobile Apps 1
I Android ListView (Custom) soll auf Hardwaretasten nicht reagieren. Android & Cross-Platform Mobile Apps 10
W ListView und Arrays... Android & Cross-Platform Mobile Apps 68
W Android Wieso kann ich keine ListView mehr zum Layout hinzufügen? Android & Cross-Platform Mobile Apps 1
W Android Kann keine ListView mehr in der MainActivtiy anzeigen, obwohl noch sehr viel Platz frei ist Android & Cross-Platform Mobile Apps 1
L ListView aktuallisiert sich nicht Android & Cross-Platform Mobile Apps 15
N Probleme mit custom dynamic ListView Android & Cross-Platform Mobile Apps 15
L Android ListView kollabiert in Scrollview Android & Cross-Platform Mobile Apps 9
A ImageButton in ListView Item bei klick ändern Android & Cross-Platform Mobile Apps 3
J Android Suche in einer ListView Android & Cross-Platform Mobile Apps 3
H Android ArrayList <-> ArrayAdapter <-> ListView Android & Cross-Platform Mobile Apps 10
L Android ListView swipe zum löschen Android & Cross-Platform Mobile Apps 1
B Android ListView set custom check Image and delete Android & Cross-Platform Mobile Apps 0
M Android ListView wird nicht dargestellt Android & Cross-Platform Mobile Apps 2
Maresuke Android Android ListView Textfarbe und Texthintergrund ändern? Android & Cross-Platform Mobile Apps 5
S Listview Einträge aus "xml" Datei Android & Cross-Platform Mobile Apps 1
S Android Studio MySql Daten in Listview mit sub Item Android & Cross-Platform Mobile Apps 11
S Textdatei in ListView einlesen Tutorial gesucht!? Android & Cross-Platform Mobile Apps 3
kaoZ Tutorial .xml Layouting für z.B ListView elemente Android & Cross-Platform Mobile Apps 7
M Android ListView und Checkbox Android & Cross-Platform Mobile Apps 6
L TableRows in ListView darstellen Android & Cross-Platform Mobile Apps 2
M ListView mit ListAdapter füllen Android & Cross-Platform Mobile Apps 5
U Android ListView Frage Android & Cross-Platform Mobile Apps 6
L Android SearchBox für Custom Listview Android & Cross-Platform Mobile Apps 5
H Android ListView Images aus dem Internet via Thread Android & Cross-Platform Mobile Apps 3
T Android: ListView-Adapter: Adapter wird ständig aufgerufen Android & Cross-Platform Mobile Apps 2
H Android SAX|ListView NullPointerException Android & Cross-Platform Mobile Apps 2

Ähnliche Java Themen

Neue Themen


Oben