我创建了一个 SharedPreferences 类,以便我的其他 Activity 可以在线使用来获取偏好。
public class SPAdapter extends Activity {
public final String preferenceSettings = "STORAGE";
// Default value if row does not exist in preference
public static final String rowExistence = "Row did not exist";
public String prefGet(String preferenceName, String rowId) {
SharedPreferences preferenceObject = getSharedPreferences(preferenceName, MODE_PRIVATE);
String value = preferenceObject.getString(rowId, rowExistence);
return value;
}
我像这样使用这种方法:
public class Splash extends Activity {
private SPAdapter spObject;
public String rowNumber(0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
spObject.prefGet(spObject.preferenceSettings,
rowNumber);
}
但是,每当我尝试使用此方法时,应用程序都会崩溃并显示有关上下文的错误。任何人都可以帮我解决这个问题吗?