2011-07-08 59 views
0

我使用SharedPreferences來存儲數據。當我嘗試從我的類的構造函數調用數據存儲函數時,它顯示運行時錯誤。但是,當我從onCreate()調用函數時,它工作正常。
有沒有什麼辦法可以從構造函數中調用我的數據存儲函數?無法訪問構造函數中的SharedPreferences

package com.examples.storedata; 

import android.app.Activity; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.View; 

import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Activity1 extends Activity 
{ 
    EditText editText1, editText2; 
    TextView textSavedMem1, textSavedMem2; 
    Button buttonSaveMem1, buttonSaveMem2; 

    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     textSavedMem1 = (TextView)findViewById(R.id.savedmem1); 
     textSavedMem2 = (TextView)findViewById(R.id.savedmem2); 
     editText1 = (EditText)findViewById(R.id.edittext1); 
     editText2 = (EditText)findViewById(R.id.edittext2); 
     buttonSaveMem1 = (Button)findViewById(R.id.save_mem1); 
     buttonSaveMem2 = (Button)findViewById(R.id.save_mem2); 
     buttonSaveMem1.setOnClickListener(buttonSaveMem1OnClickListener); 
     buttonSaveMem2.setOnClickListener(buttonSaveMem2OnClickListener); 
    } 

    public Activity1() 
    { 
     LoadPreferences(); //show error while calling from here 
    } 

    Button.OnClickListener buttonSaveMem1OnClickListener 
     = new Button.OnClickListener(){ 
      public void onClick(View arg0) { 
       SavePreferences("MEM1", editText1.getText().toString()); 
       LoadPreferences(); 
      } 
     }; 

    Button.OnClickListener buttonSaveMem2OnClickListener 
     = new Button.OnClickListener(){ 
      public void onClick(View arg0) { 
       SavePreferences("MEM2", editText2.getText().toString()); 
       LoadPreferences(); 
      } 
     }; 


    private void SavePreferences(String key, String value) 
    { 
     SharedPreferences sharedPreferences = getPreferences(MODE_APPEND); 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.putString(key, value); 
     editor.putInt("1",5); 
     editor.commit(); 
    } 

    private void LoadPreferences() 
    { 
     SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); 
     String strSavedMem1 = sharedPreferences.getString("MEM1", ""); 
     String strSavedMem2 = sharedPreferences.getString("MEM2", ""); 
     textSavedMem1.setText(strSavedMem1); 
     textSavedMem2.setText(strSavedMem2); 
    } 
} 

回答

0

您試圖訪問你的文本視圖已初始化視圖或變量之前:

textSavedMem1.setText(strSavedMem1); 
textSavedMem2.setText(strSavedMem2); 

當您從活動構造函數中調用LoadPreferencesonCreate尚未被調用,這意味着textSavedMem1textSavedMem2尚未初始化,並且爲null

當您從onCreate調用它時,只要它在以下之後,那麼這些變量將被初始化,並且所有事情都按預期工作。

setContentView(R.layout.main); 

textSavedMem1 = (TextView)findViewById(R.id.savedmem1); 
textSavedMem2 = (TextView)findViewById(R.id.savedmem2); 
+0

但事情是,當我宣佈textSavedMem1 =(TextView)findViewById(R.id.savedmem1);在構造函數中,它也不起作用。 – AndroidDev

+0

這是因爲構造函數是做錯的地方。您應該_only_初始化'onCreate'中的UI元素。它不工作的原因是因爲你沒有調用'setContentView'。現在,雖然技術上可以在構造函數中做到這一點,但我期望您還會遇到其他問題,因爲Android尚未準備好開始編寫Activity,直到它調用「onCreate」爲止。在Android調用'onCreate'之前,你幾乎沒有可以與之交互的活動。 – RivieraKid

0

它不是可能的,因爲ü可以得到在已創建活動SharedPreference ..這裏烏爾試圖讓SharedPreference甚至在活動正在建設(構造函數)。對於得到共享偏好u需要activity ..

U可以在一些實例變量存儲值裏面的構造函數,然後把它們放在SharedPreference ...

+0

這意味着我們有從構造 – AndroidDev

+0

沒有辦法稱它沒有選擇..不是之前ü創建活動..之前即onCreate()方法.. – ngesh

+0

我可以使用其他存儲方法像SQLLite內部存儲解決這個問題 – AndroidDev

0

這是正常的。在創建活動時,例如在調用構造函數時,SharePreferences對象不可訪問。使用/重寫活動的構造函數不是很好的做法。閱讀關於activity lifecycle以瞭解如何使用活動。

相關問題