S
stef-we
Neues Mitglied
- 0
Hallo,
ich erstelle gerade meine erste APP und versuche in einer Methode den Inhalt eines Buttons zu ändern:
Ich habe nur die MainActivity
und entsprechend die Layouts activity_main und fragment_main
in den On Create füge ich meine Methode ein
Das wirft natürlich Fehler, da findByView nicht funktioniert.
Ändere ich setContentView zu fragment_main, kommt kein Nullpointer mehr, allerdings wird mir auch der Text "TEST" nicht angezeigt.
Wäre super wenn mir jemand weiterhelfen kann
Gruß
Stefan
[/LEFT]
ich erstelle gerade meine erste APP und versuche in einer Methode den Inhalt eines Buttons zu ändern:
Ich habe nur die MainActivity
und entsprechend die Layouts activity_main und fragment_main
in den On Create füge ich meine Methode ein
Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]protected
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState) {[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//Layout [U]zuweisen[/U][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][SIZE=2]setContentView(R.layout.[/LEFT]
[/LEFT]
[/SIZE][LEFT][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]activity_main[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (savedInstanceState == [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]) {[/SIZE]
[SIZE=2]getFragmentManager().beginTransaction()[/SIZE]
[SIZE=2].add(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]container[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2], [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] PlaceholderFragment()).commit();[/SIZE][/LEFT]
[SIZE=2]}[/SIZE]
[SIZE=2]button_veraendern();[/SIZE]
[SIZE=2]}[/SIZE]
Code:
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] button_veraendern(){[/SIZE]
[SIZE=2]Button btn = (Button)findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]button1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[SIZE=2]btn.setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"TEST"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]}[/SIZE]
Das wirft natürlich Fehler, da findByView nicht funktioniert.
Ändere ich setContentView zu fragment_main, kommt kein Nullpointer mehr, allerdings wird mir auch der Text "TEST" nicht angezeigt.
Code:
[LEFT]protected[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState) {[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//Layout [U]zuweisen[/U][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][SIZE=2]setContentView(R.layout.[/LEFT]
[/LEFT]
[/SIZE][LEFT][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]fragment_main[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (savedInstanceState == [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]) {[/SIZE]
[SIZE=2]getFragmentManager().beginTransaction()[/SIZE]
[SIZE=2].add(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]container[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2], [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] PlaceholderFragment()).commit();[/SIZE][/LEFT]
[SIZE=2]}[/SIZE]
[SIZE=2]button_veraendern();[/SIZE]
[LEFT][SIZE=2]setContentView(R.layout.[I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]activity_main[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/LEFT]
}
[/SIZE]
Wäre super wenn mir jemand weiterhelfen kann
Gruß
Stefan
[/LEFT]