title: View becomes null on setText
body: <p>I have a custom <code>ViewGroup</code> that instantiates a private <code>TextView text1</code> in a the <code>init()</code> method called by the View constructor.</p>
  
  <p>I get a <code>NullPointerException</code> when I'm trying to <code>setText(...)</code>, on line 335, even though I specifically check that it's not null before:</p>
  
  <pre><code>//also tried instantiating here with text1 = new TextView(this.getContext());
  private TextView text1;
  
  
  public SlidingTextView(Context context, AttributeSet attrs) {
          super(context, attrs);
          init();//line 65
      }
  
  private void init() {
     ...
     //instantiate here 
     text1 = new TextView(this.getContext());
  
     //other setters on text1
     text1.setTextSize(fontSize);
     text1.setTextColor(Color.WHITE);
     text1.setPadding(textPadding, 0, textPadding, 0);
     text1.setGravity(Gravity.CENTER);
     text1.setWidth(textWidth);
     text1.setMaxLines(1);
     text1.setEllipsize(TextUtils.TruncateAt.END);
     text1.setMaxWidth(textWidth);
     text1.setBackgroundColor(Color.TRANSPARENT);
     text1.measure(0, textHeight);
  
     // Logs: android.widget.TextView@41b04b60 false
     Log.d("", text1.toString() + " " + (text1 == null));
     // Throws NullPointerException
     text1.setText("Text");//Line 335
  ...
  }
  </code></pre>
  
  <p>Here's the logcat:</p>
  
  <pre><code>07-23 12:51:11.433: E/AndroidRuntime(31419): FATAL EXCEPTION: main
  07-23 12:51:11.433: E/AndroidRuntime(31419): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: android.view.InflateException: Binary XML file line #71: Error inflating class com.example.app.SlidingTextView
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.os.Handler.dispatchMessage(Handler.java:99)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.os.Looper.loop(Looper.java:137)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.app.ActivityThread.main(ActivityThread.java:4921)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at java.lang.reflect.Method.invokeNative(Native Method)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at java.lang.reflect.Method.invoke(Method.java:511)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at dalvik.system.NativeStart.main(Native Method)
  07-23 12:51:11.433: E/AndroidRuntime(31419): Caused by: android.view.InflateException: Binary XML file line #71: Error inflating class com.example.app.SlidingTextView
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.app.Activity.setContentView(Activity.java:1924)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at com.example.app.MainActivity.onCreate(MainActivity.java:101)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.app.Activity.performCreate(Activity.java:5206)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    ... 11 more
  07-23 12:51:11.433: E/AndroidRuntime(31419): Caused by: java.lang.reflect.InvocationTargetException
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at java.lang.reflect.Constructor.constructNative(Native Method)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    ... 22 more
  07-23 12:51:11.433: E/AndroidRuntime(31419): Caused by: java.lang.NullPointerException
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.widget.TextView.checkForRelayout(TextView.java:6600)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.widget.TextView.setText(TextView.java:3732)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.widget.TextView.setText(TextView.java:3590)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at android.widget.TextView.setText(TextView.java:3565)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at com.example.app.SlidingTextView.init(SlidingTextView.java:335)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    at com.example.app.SlidingTextView.&lt;init&gt;(SlidingTextView.java:65)
  07-23 12:51:11.433: E/AndroidRuntime(31419):    ... 25 more
  </code></pre>
  
  <p>Any ideas what might be the cause of this?</p>
  
  <p><strong>UPDATE1:</strong></p>
  
  <pre><code>textWidth=500;
  textHeight=0;
  textPadding=10;
  fontSize=30;
  </code></pre>
  
  <p>All variables and values checked before setting <code>setText()</code> , not null and returning proper values</p>
  
  <p><strong>UPDATE2:</strong>
  <em>main.xml:</em></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/root"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="@drawable/background_gradient"
      tools:context="com.example.app.MainActivity" &gt;
    &lt;LinearLayout
          android:id="@+id/footer"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:gravity="center"
          android:orientation="horizontal"
          android:paddingBottom="10dp"
          android:paddingTop="10dp" &gt;
  
          &lt;ImageView
              android:id="@+id/message"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginRight="10dp"
              android:src="@drawable/message_btn" /&gt;
  
          &lt;ImageView
              android:id="@+id/start"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/start_btn" /&gt;
  
          &lt;ImageView
              android:id="@+id/call"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="10dp"
              android:src="@drawable/call_btn" /&gt;
    &lt;/LinearLayout&gt;
    &lt;com.example.app.SlidingTextView
          android:id="@+id/text"
          android:layout_width="match_parent"
          android:layout_height="30dp"
          android:layout_above="@id/footer" /&gt;    
  
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><em>MainActivity.java:</em></p>
  
  <pre><code>public class MainActivity extends Activity {
      private int height;
      private int width;
      private Display display;
  
      private RelativeLayout rootContainer;
      private SlidingTextView slidingTextView;
  
  protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
  }
  </code></pre>
  
  <p>I removed as much code as possible for testing. The problem remains.</p>
  
  <p><strong>Update3:</strong></p>
  
  <p>The problem seems to be: <code>text1.measure(0,textHeight)</code>.
  Although <code>textHeight=0</code>, it doesn't matter, I tried with different values.</p>
  
  <p>It also seems to happen only when <code>text1.measure(...)</code>  is called after the other setters:</p>
  
  <pre><code>// No NullPointerException
  text1.measure(0,textHeight);
  
  // The other setters
  text1.setTextSize(fontSize);
  text1.setTextColor(Color.WHITE);
  text1.setPadding(textPadding, 0, textPadding, 0);
  text1.setGravity(Gravity.CENTER);
  text1.setWidth(textWidth);
  text1.setMaxLines(1);
  text1.setEllipsize(TextUtils.TruncateAt.END);
  text1.setMaxWidth(textWidth);
  text1.setBackgroundColor(Color.TRANSPARENT);
  
  // Throws NullPointerException
  text1.measure(0,textHeight);
  </code></pre>
  
  <p>Can someone explain why? Probably because the text value of <code>text1</code> is <code>""</code> (never set, no call to <code>setText(...)</code> before), so after calling one of the setters, <code>text1</code>s text it is set <code>null</code> or something of the kind. I will try to circumvent the <code>measure(...)</code>, but it would be nice to get an explanation.
  Thank you!</p>