android - get applicationContext in TextView.setText() -
i'm building textview
subclass change text inserted in android:text
. instance, want capitalize whole text but, make sure needed, need access application instance (i have boolean tells if must or not capitalized).
i implemented 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 nullpointerexception here since var context 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 layout, have declared this
<my.package.uppertextview android:id="@+id/foo" android:text="bar"/>
i nullpointerexception when invoking context.getapplicationcontext()
.
did came across this?
i think should try :
this.getcontext().getapplicationcontext()
this should not return null pointer exception.
Comments
Post a Comment