Note that there are some explanatory texts on larger screens.

plurals
  1. POget applicationContext in TextView.setText()
    primarykey
    data
    text
    <p>I'm building a <code>TextView</code> subclass to change the text that is inserted in the <code>android:text</code>. 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).</p> <p>I implemented this subclass:</p> <pre><code>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 &amp;&amp; applicationContext instanceof MyApplication &amp;&amp; applicationContext.doUppercase()) { MyApplication myApp = (MyApplication) applicationContext; myApp.getLanguagesController().getLocalizedString(text.toString().toUpperCase()); } super.setText(text, type); } } </code></pre> <p>In the layout, I have it declared like this</p> <pre><code> &lt;my.package.UpperTextView android:id="@+id/foo" android:text="bar"/&gt; </code></pre> <p>I get a NullPointerException when invoking <code>context.getApplicationContext()</code>.</p> <p>Did anyone already came across this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload