我的TextView出现问题。我有一个显示耗材信息的PlayScreenActivity。在屏幕的顶部。然后我去商店的屏幕买了补给品。当我回来时所有其他人改变但是1如果我转动手机和方向改变它将显示新值,这是我能看到所购买的附加物品的唯一方式。任何人都知道为什么会这样?
我将屏幕方向设置为纵向,以查看它是否能够正常工作,如果它无法切换但无法修复。
修改
以下是我的活动代码的副本:
public class PlayscreenActivity extends Activity { 数据数据_;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((TextView)findViewById(R.id.lemonsLeftText)).setText(
"Lemons: " );
((TextView)findViewById(R.id.sugarLeftText)).setText(
"Sugar: " );
((TextView)findViewById(R.id.iceLeftText)).setText(
"Ice: " );
((TextView)findViewById(R.id.cupsLeftText)).setText(
"Cups: " );
((Button)findViewById(R.id.btnshop)).setOnClickListener(
new SupplyShopListener());
}
private class SupplyShopListener implements OnClickListener{
public void onClick(View v){
Intent i = new Intent(v.getContext(), SupplyShopActivity.class);
startActivity(i);
//refreshDisplay();
}
}
@Override
public void onResume(){
super.onResume();
data_ = getData();
refreshDisplay();
}
@Override
public void onPause(){
super.onPause();
saveData();
refreshDisplay();
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
//refreshDisplay();
}
private Data getData() {
GameData player_data = new GameData(this);
player_data.open();
Data game_state = new Data(player_data.getGameString());
player_data.close();
return game_state;
}
private void saveData() {
GameData player_data = new GameData(this);
player_data.open();
player_data.setGameString(data_.SerializeGame());
player_data.close();
}
private void refreshDisplay() {
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
String totalCash = nf.format(data_.cash_);
((TextView)findViewById(R.id.lemonsLeftText)).setText(
"Lemons: " + Integer.toString(data_.lemons_) );
((TextView)findViewById(R.id.sugarLeftText)).setText(
"Sugar: " + Integer.toString(data_.sugar_) );
((TextView)findViewById(R.id.iceLeftText)).setText(
"Ice: " + Integer.toString(data_.ice_) );
((TextView)findViewById(R.id.cupsLeftText)).setText(
"Cups: " + Integer.toString(data_.cups_) );
((TextView)findViewById(R.id.totalCashText)).setText(
"Cash : " + (totalCash) );
}
}
答案 0 :(得分:0)
您可能会在错误的位置更新textviews。
或者在更改后调用textview.invalidate()
- 这通常不是必需的,但可能是在他们更改时没有用户互动。
答案 1 :(得分:0)
...如果我转动手机并改变方向,它会显示新值
Android会在发生运行时配置更改时销毁并重新创建活动。在屏幕旋转中绘制新变量是有道理的,因为重新创建了整个活动:
http://developer.android.com/guide/topics/resources/runtime-changes.html
如果您的PlayScreenActivity恢复时大多数TextView小部件都在更新,那么您的refreshDisplay()调用正在工作(在某种程度上)。哪个小部件不重绘?我也会抛出一些日志记录并像zapl建议的那样观看LogCat。
修改强>
带有一些日志记录的示例代码(我将小部件移动到字段中......)
public class PlayscreenActivity extends Activity
{
Data data_;
String TAG = "PlayScreenActivity";
TextView lemonsLeftTextLabel;
TextView sugarLeftTextLabel;
TextView iceLeftLabel;
TextView cupsLeftTextLabel;
TextView totalCashTextLabel;
Button shopButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Creating PlayScreen activity...");
setContentView(R.layout.main);
Log.d(TAG, "Assigning buttons...");
lemonsLeftTextLabel = (TextView) findViewById(R.id.lemonsLeftText);
sugarLeftTextLabel = (TextView) findViewById(R.id.sugarLeftText);
iceLeftLabel = (TextView) findViewById(R.id.iceLeftText);
cupsLeftTextLabel = (TextView) findViewById(R.id.cupsLeftText);
totalCashTextLabel = (TextView) findViewById(R.id.totalCashText);
shopButton = (Button) findViewById(R.id.btnshop);
lemonsLeftTextLabel.setText("Lemons: ");
sugarLeftTextLabel.setText("Sugar: ");
iceLeftLabel.setText("Ice: ");
cupsLeftTextLabel.setText("Cups: ");
shopButton.setOnClickListener(new SupplyShopListener());
Log.d(TAG, "onCreate() complete");
}
private class SupplyShopListener implements View.OnClickListener {
public void onClick(View v){
//Intent i = new Intent(v.getContext(), SupplyShopActivity.class);
//startActivity(i);
refreshDisplay();
}
}
@Override
public void onResume(){
Log.d(TAG, "onResume() called");
super.onResume();
data_ = getData();
Log.d(TAG, "Data fetched");
Log.d(TAG, "Contents of data = " +
"Lemons: " + Integer.toString(data_.lemons_) + " | " +
"Sugar: " + Integer.toString(data_.sugar_) + " | " +
"Ice: " + Integer.toString(data_.ice_) + " | " +
"Cups: " + Integer.toString(data_.cups_) + " | " +
"Cash: " + data_.cash_ + " | ");
refreshDisplay();
}
@Override
public void onPause(){
Log.d(TAG, "onPause() called");
super.onPause();
saveData();
refreshDisplay();
}
@Override
public void onConfigurationChanged(Configuration newConfig){
Log.d(TAG, "onConfigurationChanged() called");
super.onConfigurationChanged(newConfig);
}
private Data getData() {
Log.d(TAG, "getData() called - restoring");
GameData player_data = new GameData(this);
player_data.open();
Log.d(TAG, "Pulling data from game string...");
Data game_state = new Data(player_data.getGameString());
player_data.close();
Log.d(TAG, "Contents of data = " +
"Lemons: " + Integer.toString(game_state.lemons_) + " | " +
"Sugar: " + Integer.toString(game_state.sugar_) + " | " +
"Ice: " + Integer.toString(game_state.ice_) + " | " +
"Cups: " + Integer.toString(game_state.cups_) + " | " +
"Cash: " + game_state.cash_ + " | ");
return game_state;
}
private void saveData() {
Log.d(TAG, "saveData() called - saving");
GameData player_data = new GameData(this);
player_data.open();
Log.d(TAG, "Serializing data into GameData object");
Log.d(TAG, "Contents of data = " +
"Lemons: " + Integer.toString(data_.lemons_) + " | " +
"Sugar: " + Integer.toString(data_.sugar_) + " | " +
"Ice: " + Integer.toString(data_.ice_) + " | " +
"Cups: " + Integer.toString(data_.cups_) + " | " +
"Cash: " + data_.cash_ + " | ");
player_data.setGameString(data_.SerializeGame());
player_data.close();
}
private void refreshDisplay() {
Log.d(TAG, "refreshDisplay() called - redrawing UI");
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
String totalCash = nf.format(data_.cash_);
lemonsLeftTextLabel.setText("Lemons: " + Integer.toString(data_.lemons_));
sugarLeftTextLabel.setText("Sugar: " + Integer.toString(data_.sugar_));
iceLeftLabel.setText("Ice: " + Integer.toString(data_.ice_));
cupsLeftTextLabel.setText("Cups: " + Integer.toString(data_.cups_));
totalCashTextLabel.setText("Cups: " + totalCash);
}
}