2015-07-03 61 views
0

我有錯誤「mTvCreate不能resoved」,同時設立的Android TextView_Instance.setText()錯誤

mTvCreate.setText("onCreate() calls: " + mCreate); 

這低於

給出的最後四個語句具有相同的錯誤代碼之一

也請任何人都可以解釋

  1. 爲什麼在第1行語句的代碼下面4,我們定義爲RESTART_KEY字符串,並將其指定爲「重啓」什麼是它的功能。

  2. 什麼聲明「savedInstanceState.putInt(CREATE_KEY,mCreate);」在 public void onSaveInstanceState(Bundle savedInstanceState)方法會做。這種方法只有在應用程序要離開當前活動並進入另一活動時才被調用來保存狀態信息。

謝謝。

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"; 

int mCreate, mRestart, mStart, mResume; 

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

if (savedInstanceState != null) { 

      // TODO: 
      // Restore value of counters from saved state 
      // Only need 4 lines of code, one for every count variable 
      //savedInstanceState.getInt("mCreate", mCreate); 
      //savedInstanceState.getInt("mRestart", mRestart); 
      //savedInstanceState.getInt("mStart", mStart); 
      //savedInstanceState.getInt("mResume", mResume); 

      mStart = savedInstanceState.getInt(START_KEY); 
      mCreate = savedInstanceState.getInt(CREATE_KEY); 
      mResume = savedInstanceState.getInt(RESUME_KEY); 
      mRestart = savedInstanceState.getInt(RESTART_KEY); 


     } 

public void onSaveInstanceState(Bundle savedInstanceState) { 
     // TODO: 
     // Save state information with a collection of key-value pairs 
     // 4 lines of code, one for every count variable 
     super.onSaveInstanceState(savedInstanceState); 

     savedInstanceState.putInt(CREATE_KEY, mCreate); 
     savedInstanceState.putInt(RESTART_KEY, mRestart); 
     savedInstanceState.putInt(RESUME_KEY, mResume); 
     savedInstanceState.putInt(START_KEY, mStart); 



    } 

public void displayCounts() { 

     // TODO - uncomment these lines 

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

是不是你的代碼? – Elltz

+0

我是學生,新來android和學習它,請你能幫我解決我在Eclipse中在mTvCreate.setText(「onCreate()調用:」+ mCreate)中收到的錯誤;聲明。 – Umar

回答

1

您錯過了您的OnCreate。沒有它,什麼都不會被初始化。看起來好像你從某處複製了這段代碼。首先閱讀官方文檔以開始使用Android。 https://developer.android.com/training/index.html

  1. 這些常數,這樣就可以使用RESTART_KEY整個代碼,而無需編寫了「重啓」每次

  2. 參考文檔,以獲得活動的生命週期有充分的瞭解。

+0

謝謝,我已經複製了我正在使用的賦值activity_one.java文件中的代碼及其onCreate()方法的一部分。對於問題1,恢復,啓動,啓動,創建變量會自動生成?這些活動生命週期變量是什麼? – Umar