2013-07-14 77 views
0

我是OOP和Android中的noobie,我面臨的是一個讓我非常沮喪的小問題。 我正在創建一個使用永久存儲的應用程序。 最初,我創建了訪問保存的首選項的代碼,這些首選項都混合到了MainActivity中,然後我想將該代碼移動到一個單獨的類文件中。 問題是,由於某種原因,它不會在分離的類文件中工作,並嘗試並嘗試後,我發現我可以在MainActivity類中創建一個內部類,並以這種方式工作。 我相信這是與事實有關,如果我創建它作爲內部類,我不需要讓內部類擴展活動(再次)。 當爲永久存儲處理創建一個外部類時,我需要在該類上擴展Activity,我認爲這是問題,但我不確定。 有人可以請向我解釋爲什麼發生這種情況,也許建議正確的做法? 以下我包括一個可用的代碼片段,但我的目標是能夠在分離的類文件中創建類PermanentStorageHelper。 在此先感謝!將功能提取到單獨的類文件不起作用

public class MainActivity extends Activity { 

public static MainActivity _mainActivity; 
private TextView textView1; 

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

    // Persistent preferences 
    PermanentStorageHelper ps = new PermanentStorageHelper(); 


    // UI Initialization 
    textView1 = (TextView) findViewById(R.id.textView1); 

    String uId = ps.getuId(); 
    UiHelper.displayOnTextView(this, R.id.textView1, uId); 

} 



// ============================================= 
// This is the class I'm talking about, I'm unable to move this to 
// a separated class (.java) file. 
// It seems to be related to the fact that, if making this a separated 
// class file, I need to extend Activity again and that is what 
// seems to be the problem 
// ============================================= 
public class PermanentStorageHelper /*extends Activity*/{ 

    // CONSTANTS 
    public static final String USERUNIQUEID="userUniqueID";    // Saved setting 1 
    public static final String FILENAME="mtcPreferences";    // Filename for persisting storage file 
    // Fields 
    public SharedPreferences shp;          // SharedPreferences field (1) 
    public String uId; 


    public PermanentStorageHelper(){ 

     // Preferences initialization (2) 
     shp = getSharedPreferences(FILENAME, MODE_PRIVATE); 

     // Read Preferences (3) 
     uId = shp.getString(USERUNIQUEID, null); 
    } 

    // Getters and Setters 
    public String getuId() { 
     return uId; 
    } 
    public void setuId(String uId) { 
     this.uId = uId; 
    } 

} 
+0

當你移動該類到另一個文件,你寫在'擴展活動'行在它或不? – Navid777

+1

您不應手動實例化擴展Activity的類(只有平臺代碼知道如何正確執行所需的所有操作)。所提供的答案指出瞭如何讓你的代碼工作,而不會讓你的助手類錯誤地擴展Activity本身。 –

回答

2

通過上下文來新類:

public PermanentStorageHelper(Context context){ 

    // Preferences initialization (2) 
    shp = context.getSharedPreferences(FILENAME, MODE_PRIVATE); 

} 

然後你就可以創建類,如:

new PermanentStorageHelper(MainActivity.this) 
+0

嗨,我還應該在新課程中擴展Activity嗎? @maclir – user2566468

+0

謝謝大家,它適用於您的建議方法,但我仍然需要在新類中擴展Activity才能使其工作,如果不能,我會得到編譯時錯誤 – user2566468

+0

不,請不要擴展活動。你的新錯誤是什麼?這可能是一個不同的問題。 –

0

getSharedPreferences你需要有訪問activityapplicationContext

你可以添加上下文到你的const ructor並用它打電話getSharedPreferences

在這種情況下
public PermanentStorageHelper(Context context){ 

     // Preferences initialization (2) 
     shp = context.getSharedPreferences(FILENAME, MODE_PRIVATE); 

     // Read Preferences (3) 
     uId = shp.getString(USERUNIQUEID, null); 
} 

你需要通過它創建對象的實例時上:

PermanentStorageHelper ps = new PermanentStorageHelper(getApplicationContext()); 

PermanentStorageHelper ps = new PermanentStorageHelper(MainActivity.this);