Dialog in Fragment

Javandroid

Mitglied
Hallo zusammen,

Ich versuche aktuell ein Dialog, zur Eingabe eines Users aus einem Fragment heraus aufzurufen, leider aktuell ohne Erfolg. Bei google finde Ich leider nur Beispiele zum Starten eines Dialogs aus einer Activity heraus.

Hat jemand diesbezüglich ein Beispiel parat?
Vielen Dank vorab und Gruß
 

dzim

Top Contributor
Was genau ist das Problem? Das Fragment kennt seine Activity. Dafür gibt es die Methode #getActivity() - und von dort aus kannst du wie gewohnt arbeiten...
 

Javandroid

Mitglied
Der Dialog öffnet sich nun,. Wenn Ich aber eine Eingabe im Dialog gemacht habe und den TextView des Fragment dadurch ändern möchte bekomme Ich eine NullPointerException, sieht jemand warum?

Java:
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());

			View view = layoutInflater.inflate(R.layout.xy);
			final TextView textView = (TextView) view.findViewById(R.id.tv_yz);		
		
			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());

			alertDialogBuilder.setView(view);

			final EditText inputText = (EditText) view.findViewById(R.id.editText1);

			
			alertDialogBuilder.setTitle("Title:");
			
			// setup a dialog window
			alertDialogBuilder
					.setCancelable(false)
					.setPositiveButton("OK", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int id) {
									// get user input and set it to result
									textView.setText(inputText.getText());
								}
							})
					.setNegativeButton("Cancel",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,	int id) {
									dialog.cancel();
								}
							});

			// create an alert dialog
			AlertDialog alertD = alertDialogBuilder.create();

			alertD.show();
 

dzim

Top Contributor
Wollte gerade was anderes schreiben, aber ich hab meine Meinung geändert :-D

Ich würde dir folgendes Empfehlen: Verwende ein Dialog-Fragment. Lässt sich in weiten Teilen wie ein Dialog steuern, aber die Logik ist weit einfacher zu implementieren. Funktioniert genauso wie dein Fragment, aus dem heraus du den Dialog öffnest.

Using DialogFragments | Android Developers Blog
DialogFragment | Android Developers

https://github.com/codepath/android_guides/wiki/Using-DialogFragment

......... u.s.w.u.s.f. ........
 

Javandroid

Mitglied
Danke. Eigentlich funktioniert ja alles soweit. Wenn Ich nach "OK" den Text via Toast anzeigen lassen zeigt er den eingegebenen Text auch korrekt an. Möchte Ich aber die TextView unter dem Dialog nach "OK" ändern...
Java:
textView.setText(inputText.getText().toString());
...schmeißt er die NullPointerException. Da Ich was dazu lernen möchte die Frage warum das so ist und nicht funktioniert...
 
Zuletzt bearbeitet:

Javandroid

Mitglied
Ich habe via findViewById() einen Wert eines anderen Layouts zugewiesen und dann kommt natürlich null dabei raus.
Vielen Dank, das Problem ist somit gelöst. Manchmal hängt man an Kleinigkeiten die eigentlich Offensichtlich sind. Aber aus Fehlern lernt man bekanntlich ja am meisten.

Thx
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
R Android Dialog verstecken, statt zu schliessen Android & Cross-Platform Mobile Apps 7
J Input Dialog - ist das so richtig ? Android & Cross-Platform Mobile Apps 1
L Leerer Dialog Android & Cross-Platform Mobile Apps 0
L Dialog anzeigen wenn auf Button gedrückt wird. Android & Cross-Platform Mobile Apps 4
B Android Alert Dialog mit sound oder Android & Cross-Platform Mobile Apps 1
B Senden via Dialog? Android & Cross-Platform Mobile Apps 6
K Android Activity for result aus Dialog Android & Cross-Platform Mobile Apps 1
N Android Retain Dialog verschwindet beim drehen Android & Cross-Platform Mobile Apps 4
T Android - Toast Dialog oder was anders? Android & Cross-Platform Mobile Apps 3
A Android Probleme mit Dialog Android & Cross-Platform Mobile Apps 4
A Android Dialog wird nicht sofort angezeigt Android & Cross-Platform Mobile Apps 12
L Custom Dialog Button event Android & Cross-Platform Mobile Apps 2
I modaler Dialog Android & Cross-Platform Mobile Apps 2
W Relativelayout in Fragment Android & Cross-Platform Mobile Apps 46
W onViewCreated blockiert Session Zugriff gegensatz zu onCreateView? Fragment Android & Cross-Platform Mobile Apps 25
CT9288 Methode von Fragment durch Activity rufen scheitert Android & Cross-Platform Mobile Apps 7
W Bild aus dem Internet in View bzw. ImageView laden (Fragment) Android & Cross-Platform Mobile Apps 2
Noahscript Android Verhindern, dass Fragment Transaktion die UI blockt Android & Cross-Platform Mobile Apps 7
L Android Leiste unter Fragment Android & Cross-Platform Mobile Apps 2
P Herausfinden, welches Fragment gerade angezeigt wird. Android & Cross-Platform Mobile Apps 1
N Android Retain-Fragment + ActionBar-Tabs = Absturz!? Android & Cross-Platform Mobile Apps 9
D Android Fragment/FragmentActivity in Popupwindow Android & Cross-Platform Mobile Apps 4

Ähnliche Java Themen

Neue Themen


Oben