屏幕旋转后TextView消失

时间:2013-08-09 20:42:19

标签: java android textview lifecycle

我是savedInstanceState的新手。那里有一些很棒的资源,我几乎已经把它全部搞清楚了。但是,我的TextView更新就像我想要的那样,但它消失了。我希望它在我旋转设备时保持在那里,而不是必须单击按钮才能将其恢复。我知道哪里出错了?谢谢!

TextView cashView是我想看到现金的地方。

@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);
      // Save UI state changes to the savedInstanceState.
      // This bundle will be passed to onCreate if the process is
      // killed and restarted.
      savedInstanceState.putInt("cash", cash);
      savedInstanceState.putInt("anteBet", anteBet);
      savedInstanceState.putInt("playBet", playBet);
      savedInstanceState.putInt("pairPlusBet", pairPlusBet);
      savedInstanceState.putString("cashView", cashView.toString());
      // etc.
    }

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  cash = savedInstanceState.getInt("cash");
  anteBet = savedInstanceState.getInt("anteBet");
  playBet = savedInstanceState.getInt("playBet");
  pairPlusBet = savedInstanceState.getInt("pairPlusBet");
  cashView.setText(savedInstanceState.getString(cashView.toString()));
}

1 个答案:

答案 0 :(得分:0)

哈哈......经过一个小时的摆弄之后,我一说完这个问题便想出来了。谢谢你的时间!

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putInt("cash", cash);
  savedInstanceState.putInt("anteBet", anteBet);
  savedInstanceState.putInt("playBet", playBet);
  savedInstanceState.putInt("pairPlusBet", pairPlusBet);
  savedInstanceState.putString("cashView", cashView.getText().toString());
  // etc.
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  cash = savedInstanceState.getInt("cash");
  anteBet = savedInstanceState.getInt("anteBet");
  playBet = savedInstanceState.getInt("playBet");
  pairPlusBet = savedInstanceState.getInt("pairPlusBet");
  cashView.setText(savedInstanceState.getString("cashView"));
}
我改变了       savedInstanceState.putString(“cashView”,cashView.toString()); 至       savedInstanceState.putString(“cashView”,cashView.getText()。toString());

我改变了   cashView.setText(savedInstanceState.getString(cashView.toString())); 至   cashView.setText(savedInstanceState.getString( “cashView”));

不确定是哪一个修复了它,但它是固定的。不管怎样,谢谢!