我想从另一个不是活动的类更新TextView,但应用程序一直在粉碎.. 我希望你能帮忙
这是我想在了解游戏结果后更新文字的课程
public class GameLogic {
...
public void gameResult() {
OnePlayerGameActivity gameActivity = new OnePlayerGameActivity();
TextView result = (TextView) gameActivity.findViewById(R.id.game_result_textView);
getPlayerChoice();
int computer = computerChoice();
if (mPlayerChoice == computer) {
mPlayerStat += 0;
mOpponentChoice += 0;
} else if (mPlayerChoice == 1) { //ROCK
switch (computer) {
case 1: //ROCK
break;
case 2: //PAPER
mOpponentStat ++;
result.setText("LOST");
break;
...
所有代码都可以在GitHub
上找到答案 0 :(得分:1)
您正在创建活动的虚拟实例,该实例与内存中的实例无关,而是使用
传递活动实例// in your OnePlayerGameActivity.java
// inside some method
GameLogic obj = new GameLogic (); // instance of GameLogic for demo
obj.gameResult(OnePlayerGameActivity.this); // pass instance
并将方法签名更改为
public void gameResult(OnePlayerGameActivity gameActivity ) {
TextView result = (TextView) gameActivity.findViewById(R.id.game_result_textView);