2012-03-20 104 views
3

我遇到了問題與我的TextView。我有一個顯示耗材信息的PlayScreenActivity。在屏幕的頂部。然後我去商店屏幕和購買用品。當我回來的時候所有其他人都改變了,但是如果我改變我的手機和方向,它會顯示新的值,這是我可以看到添加的耗材購買的唯一方法。任何人都有一個想法,爲什麼這發生?在TextView中的問題android

我將屏幕方向設置爲縱向,以查看它是否能工作,如果它無法切換但無法修復。

編輯

這是我的活動代碼的副本:

公共類PlayscreenActivity延伸活動{ 數據data_中;

/** 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

...如果我把我的手機和方向變化,它會顯示新價值

Android將會破壞和發生運行時配置更改時重新創建活動。這是有道理的,你的新變量被繪製在屏幕上旋轉,因爲整個活動是重新創建:

http://developer.android.com/guide/topics/resources/runtime-changes.html

如果你的大多數的TextView小部件的更新時,您的PlayScreenActivity恢復,您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); 

    } 
} 
+0

糖不重繪。 – 2012-03-21 00:25:30

+0

好吧,我跑了Sugar的catlog,我在商店活動和遊戲屏幕上登錄。當我點擊按鈕購買糖時,我在該活動中有一個refreshTotal,顯示在我接受並返回到遊戲屏幕之前的總購買量。它顯示我的data_.sugar_獲得了12個,但是一旦它返回到播放屏幕後就沒有了。 – 2012-03-21 00:48:49

+0

嗯,我不確定Data和GameData對象,但是您選擇在saveData()中創建一個新的GameData對象。 ,然後你傳遞整個活動,這似乎有點過分。如果您只想保留數據,請嘗試傳遞一個Bundle而不是? 此外,你可以嘗試使用onRetainNonConfigurationInstance()來實際持久化一個完整的對象。它不會超越記憶,但它可能是一個有用的選擇? http://android-developers.blogspot.ca/2009/02/faster-screen-orientation-change.html – aheinrich 2012-03-21 01:22:35

0

您更新在錯誤的地方也許textviews。 或在您更改之後致電textview.invalidate() - 這通常不是必需的,但可能是在用戶更改時沒有用戶交互。

+0

但是我有3個是改變/更新其他textviews和他們改變就好了。我正在使用refreshDiplay();在onResume – 2012-03-20 23:05:55

+0

添加代碼不工作到您的問題請 – zapl 2012-03-20 23:08:05

+0

我甚至試圖移動XML設計中的TextViews和從未移動,當我重新運行我的程序 – 2012-03-20 23:16:32