2015-05-23 55 views
0

我在onCreate()方法中添加了一個條件來檢查是否存在一些以前的數據。但它不起作用。 請指導我。 我不希望使用onRestoreSavedState()方法this.d定位變化時的數據保存

public class ActivityOne extends Activity { 

使用這些當你重新配置之間的節能狀態

private static final String RESTART_KEY = "restart"; 
private static final String RESUME_KEY = "resume"; 
private static final String START_KEY = "start"; 
private static final String CREATE_KEY = "create"; 

字符串logcat的文檔鍵

private final static String TAG = "Lab-ActivityOne"; 
private int mCreate=0, mRestart=0, mStart=0, mResume=0; 

當您調用相應的生命週期方法時,您需要增加這些變量的值。

TODO:每個TextViews的創建變量

private TextView mTvCreate, mTvRestart, mTvStart, mTvResume; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_one); 

TODO:分配適當的TextViews到TextView的變量

mTvCreate=(TextView)findViewById(R.id.create); 
    mTvRestart=(TextView)findViewById(R.id.restart); 
    mTvResume=(TextView)findViewById(R.id.resume); 
    mTvStart=(TextView)findViewById(R.id.start); 


    Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo); 
    launchActivityTwoButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      Intent intent=null; 
      intent = new Intent(v.getContext(),ActivityTwo.class); 
     startActivity(intent); 

     } 
    }); 
    if(savedInstanceState!=null){ 
     mCreate=savedInstanceState.getInt(String.valueOf(mCreate)); 
     mRestart=savedInstanceState.getInt(String.valueOf(mRestart)); 
     mResume=savedInstanceState.getInt(String.valueOf(mResume)); 
     mStart=savedInstanceState.getInt(String.valueOf(mStart)); 
    } 


    // Emit LogCat message 
    Log.i(TAG, "Entered the onCreate() method"); 
     mCreate++; 
     displayCounts(); 
} 

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 

鍵 - 值對的集合保存狀態信息 4代碼行,每個計數變量一行 super.onSaveInstanceState(savedInstanceState);

savedInstanceState.putInt(String.valueOf(mCreate),mCreate); 
    savedInstanceState.putInt(String.valueOf(mRestart),mRestart); 
    savedInstanceState.putInt(String.valueOf(mResume),mResume); 
    savedInstanceState.putInt(String.valueOf(mStart),mStart); 

} 

更新顯示計數器 該方法要求櫃檯,TextView的變量使用上述 公共無效displayCounts(){

mTvCreate.setText("onCreate() calls: " + mCreate); 
    mTvStart.setText("onStart() calls: " + mStart); 
    mTvResume.setText("onResume() calls: " + mResume); 
    mTvRestart.setText("onRestart() calls: " + mRestart); 

} 

}

回答

1

它看起來像問題指定的名稱是當您嘗試捕獲保存的值時,您試圖使用尚未設置的值。

您只需要使用您定義的字符串常量就可以解決這個問題。

所以,在onCreate(),你會:

if(savedInstanceState!=null){ 
    mCreate=savedInstanceState.getInt(CREATE_KEY); 
    mRestart=savedInstanceState.getInt(RESTART_KEY); 
    mResume=savedInstanceState.getInt(RESUME_KEY); 
    mStart=savedInstanceState.getInt(START_KEY); 
} 

然後,在onSaveInstanceState(),你會:

savedInstanceState.putInt(CREATE_KEY,mCreate); 
savedInstanceState.putInt(RESTART_KEY,mRestart); 
savedInstanceState.putInt(RESUME_KEY,mResume); 
savedInstanceState.putInt(START_KEY,mStart); 
相關問題