TextView / Button /... in Methode verändern

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
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]
 
schmeiß mal den ganzen fragment code teil raus, das brauchst nicht am anfang

dann rufst als aller erstes setcontentview auf
mit dem layout, welches deinen button beinhaltet

dann kannst den button finden (findviewbyid) und verändern
 
super...danke für die schnelle Hilfe
 

Ähnliche Themen

A
  • AnimaAngelo85
Antworten
1
Aufrufe
337
swa00
swa00
MES
Antworten
10
Aufrufe
819
MES
MES
A
Antworten
1
Aufrufe
583
swa00
swa00
Zurück
Oben Unten