我是使用Eclipse在Android上創建應用程序的新手,並且在執行某些操作時遇到了一些麻煩。Android和Eclipse全局函數
我目前正在努力不必一遍又一遍地重複任務,所以可以從任何活動中調用函數。
使用公共類創建一個新的java文件看起來工作正常,但某些事情不起作用。
package com.android.packagename;
import android.content.SharedPreferences;
public class Functions
{
public static final String PREFS_NAME = "PrefsFile";
SharedPreferences preferences;
public void loadPreferences()
{
SharedPreferences preferences=getSharedPreferences(PREFS_NAME,0);
String username = preferences.getString("username", "");
}
}
從哪裏獲得的"The method getSharedPreferences(String, int) is undefined for the type Functions"
錯誤和Functions.loadPreferences();
此代碼調用它絕對是精原活動時。我試過添加這個。等開始,但沒有這樣的運氣。我錯過了什麼?
由於
閱讀關於靜態函數和關於Java的書籍。當使用靜態函數的loadPreferences時,Functions.loadPreferences()會很好。因此總結使用:static public void lP()或Functions f = new Functions(); f.lP()。 – gregory561
您可能還想閱讀此問題(http://stackoverflow.com/q/708012/778427)。這樣,你的全球班將有一個「上下文」。 –