我在这样的主要活动中有 SharedPreferences :
public class MainActivity extends SherlockFragmentActivity {
public void showUserSettings() {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
Editor editor= sharedPrefs.edit();
editor.putString("userFormat", sharedPrefs.getString("example_list", "2"));
editor.commit();
}
现在我想从 SharedPreferences 中获得价值,但在下面的课程中:
public class ReadFormatSharePerf extends Activity {
private DecimalFormat df;
public String finalFormat(String finalFormat) {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
String customFormat = sharedPrefs.getString("userFormat", "#.##");
df = new DecimalFormat(customFormat);
String result = df.format(finalFormat);
return result;
}
我像这样的代码调用了上面的活动:
public class Angles {
...
...
ReadFormatSharePerf my= new ReadFormatSharePerf();
result=my.finalFormat(String.valueOf(finalValue));
}
我发现了错误:
10-16 15:57:24.035: E/AndroidRuntime(10792): java.lang.NullPointerException
atandroid.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
我该怎么做?