开发者

SharedPreferences method is undefined for AChartEngine intents

I have used the SharedPreferences method all over my app to easily pass preference and other data. Now I'\m trying to interface with AChartEngine and I'm getting this syntax error: "The method SharedPreferences(String, int) is undefined for the type AverageHoleScoreToPar". This is the code:

...
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Paint.Align;

public class AverageHoleScoreToPar extends AbstractDemoChart {
public String getName() {
    return "getName";
}
public String getDesc() {
    return "The average temperature in 4 Greek islands (line chart)";
}
public Intent execute(Context context) {
    String PREFS_NAME = "PersistentData";    
    SharedPreferences prefs = SharedPreferences(PREF开发者_如何转开发S_NAME, 0);
    SharedPreferences.Editor editor = prefs.edit();
    int p1R_id = prefs.getInt("prefPrefp1R_id", 0);
    String p1R_name = prefs.getString("prefPrefp1R_name", "");
...

Everytime I resolve one of Android or Java's well documented intricacies, I bump into another. But it's fun (most of the time) searching for the answer.


Try this code:

SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

instead of SharedPreferences(PREFS_NAME, MODE_PRIVATE);

If that still doesn't work for AverageHoleScoreToPar, try to extend an Application class and store your SharedPreferences either in a public static SharedPreferences variable or just a public variable that you later get through getApplicationContext();

Here's an example with static:

TestApplication.java:

public class TestApplication extends Application
{
    public static SharedPreferences mPrefs;

    @Override
    public void onCreate()
    {
        super.onCreate();
        String PREFS_NAME = "PersistentData";    
        SharedPreferences mPrefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    }
}

and in your AverageHoleScoreToPar.java, use this:

public Intent execute(Context context) {
    SharedPreferences prefs = TestApplication.mPrefs;
    SharedPreferences.Editor editor = prefs.edit();
...
}

Don't forget to include the Application class in your AndroidManifest.xml file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜