I'm building a TextView
subclass to change the text that is inserted in the android:text
. For instance, I want to capitalize the whole text but, to make sure that it is needed, I need to access the Application instance (I have a boolean that tells if it must or not be capitalized).
I implemented this subclass:
public class UpperTextView extends TextView {
private Context context;
public UpperTextView(Context context) {
super(context);
this.context = context;
}
public UpperTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
public UpperTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
@Override
public void setText(CharSequence text, BufferType type) {
//I get a NullPointerException here since var context is null
Context applicationContext = context.getApplicationContext();
if (text != null && applicationContext instanceof MyApplication && applicationContext.doUppercase()) {
MyApplication myApp = (MyApplication) applicationContext;
myApp.getLanguagesController().getLocalizedString(text.toString().toUpperCase());
}
super.setText(text, type);
}
}
In the layout, I have it declared like this
<my.package.UpperTextView
android:id="@+id/foo"
android:text="bar"/>
I get a NullPointerException when invoking context.getApplicationContext()
.
Did anyone already came across this?