Fragments und Button

C

chazon

Neues Mitglied
0
Hallo Leute

Jetzt habe ich doch mal eine Frage
Ich sitze gerade an einer App in welcher ich einen ViewPager verwende
also eine Activity und mehrere Fragments.
Jetzt möchte ich in den einzelnen Fragments einen Button implementieren,
welche eine neue Activity öffnen .
1. Geht so etwas überhaupt
2. Wo deklariere ich die Buttons ( alle in der Activity oder einzeln in den Fragments)
3) Wo behandele ich die Ereignisse ( SetOnClick ...)

Ich hatte einmal folgendes versucht aber ich erhalte kein Ergebnis
(Code befindet sich im Fragment)

Code:
[SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]
  [LEFT]@Override[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE] [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] View onCreateView(LayoutInflater inflater, ViewGroup container,[/SIZE][/LEFT]
[SIZE=2]  [LEFT]Bundle savedInstanceState) {[/LEFT]
[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][/COLOR][/SIZE] [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (container == [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2])[/SIZE][/LEFT]
[SIZE=2] [LEFT]{[/LEFT]
[/SIZE] [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE][/LEFT]
[SIZE=2] [LEFT]}[/LEFT]
 [LEFT] [/LEFT]
 [LEFT]View view = inflater.inflate(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]fpm_samstag[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2], container, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]false[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]);[/SIZE][/LEFT]
[SIZE=2]  [LEFT]Button btn_zug_sa = (Button) view.findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]btn_zug_sa[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/LEFT]
[SIZE=2]  [LEFT]btn_zug_sa.setOnClickListener([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]);[/SIZE][/LEFT]
[SIZE=2]  [/SIZE] [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (LinearLayout) inflater.inflate(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]fpm_samstag[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2], container, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]false[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]);[/SIZE][/LEFT]
[SIZE=2]  [LEFT]}[/LEFT]
 [/SIZE] [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Btn_clic()[/SIZE][/LEFT]
[SIZE=2] [LEFT]{[/LEFT]
 [LEFT]Toast.[I]makeText[/I](getActivity(),[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Hallo"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE][/LEFT]
[SIZE=2] [LEFT]startActivity([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Intent([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"de.stalort.schuetzenfestzons2014.Zugweg_sa"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]));[/SIZE][/LEFT]
[SIZE=2] [LEFT]}[/LEFT]
 [/SIZE] [LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onClick(View v) {[/SIZE][/LEFT]
[SIZE=2]  [LEFT]Toast.[I]makeText[/I](getActivity(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Hallo"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE][/LEFT]
[SIZE=2] [/SIZE] [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]switch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](v.getId())[/SIZE][/LEFT]
[SIZE=2] [LEFT]{[/LEFT]
[/SIZE] [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]btn_zug_sa[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]:[/SIZE][/LEFT]
[SIZE=2] [LEFT]Btn_clic();[/LEFT]
[/SIZE] [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE][/LEFT]
[SIZE=2] [LEFT]}[/LEFT]
  }
 [/SIZE]
weder kommt ein Toast noch wird die neue Seite aufgerufen.

Vielleicht könnt Ihr mir helfen

Danke und Gruß
Chazon
 
Zuletzt bearbeitet:
Du hast das prinzipiell richtig gemacht, nur eine Sache ist falsch.

Du hohlst dir deinen Button aus dem view den du inflatest hast (richtig)
du gibst diesem Button einen listener(richtig)

du erstellst den view neu und gibst diesesn zurück(falsch)

der Button den du jetzt erstellt hast gehört also zu einem View der nie angezeigt wurde. Daher kommt auch kein Toast.

gib den View "view" zurück und erzeuge keinen neuen.
ach und btw warum castest du den view auf ein LinearLayout? Die Methode gibt einen View zurück ;)
 
Danke für die Antwort.

Manche Fehler sind Haarsträubend. Habe 100Mal darüber gelesen und den Fehler nicht gesehen

Klar return view war genau richtig

Hatte noch einen Fehler bei StartActivity
muss im Fragment heißen:
Code:
[SIZE=2]
 startActivity([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Intent(getActivity(), Zugweg_sa.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]));
 [/SIZE]
Dann funktioniert auch alles

Vielen Dank und
Gruß Chazon
 

Ähnliche Themen

A
  • AnimaAngelo85
Antworten
1
Aufrufe
343
swa00
swa00
MES
Antworten
10
Aufrufe
831
MES
MES
M
  • MikelKatzengreis
Antworten
5
Aufrufe
158
swa00
swa00
Zurück
Oben Unten