getsharedpreferences from custom edittext context
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
You can try this. This way you can get preferences from any part of code.
精彩评论