0

I have a custom edittext class I have made that I'd like to retrieve some sharedpref values from a prefs xml. I am currently getting the context passed on the constructor and running getsharedpreferences off that. This crashes the app, though. How would I go about reading the values from there?

Code snippet:

public class RichEditText extends EditText {
        Context acontext;
        SharedPreferences synpref;
    public RichEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        acontext=context;
        synpref = acontext.getSharedPreferences("synexp", 0);
        Log.v("MyApp",synpref.getString("exp", "value"));
    }
} 

Logcat:

08-20 20:57:06.580: ERROR/AndroidRuntime(13343): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eyecreate/com.eyecreate.MyActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class com.eyecreate.RichEditText
08-20 20:57:06.580: ERROR/AndroidRuntime(13343): Caused by: java.lang.reflect.InvocationTargetException 08-20 20:57:06.580: ERROR/AndroidRuntime(13343): Caused by: java.lang.NullPointerException The Null pointer exception was on the line with getSharedPreferences

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
eyecreate
  • 1,017
  • 3
  • 14
  • 26

1 Answers1

1

You can try this. This way you can get preferences from any part of code.

Community
  • 1
  • 1
Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43