I have a custom ViewGroup that instantiates a private TextView text1 in a the init() method called by the View constructor.
I get a NullPointerException when I'm trying to setText(...), on line 335, even though I specifically check that it's not null before:
//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
...
}
Here's the logcat:
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.<init>(SlidingTextView.java:65)
07-23 12:51:11.433: E/AndroidRuntime(31419): ... 25 more
Any ideas what might be the cause of this?
UPDATE1:
textWidth=500;
textHeight=0;
textPadding=10;
fontSize=30;
All variables and values checked before setting setText() , not null and returning proper values
UPDATE2: main.xml:
<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" >
<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" >
<ImageView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="@drawable/message_btn" />
<ImageView
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/start_btn" />
<ImageView
android:id="@+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/call_btn" />
</LinearLayout>
<com.example.app.SlidingTextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_above="@id/footer" />
</RelativeLayout>
MainActivity.java:
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);
}
I removed as much code as possible for testing. The problem remains.
Update3:
The problem seems to be: text1.measure(0,textHeight).
Although textHeight=0, it doesn't matter, I tried with different values.
It also seems to happen only when text1.measure(...) is called after the other setters:
// 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);
Can someone explain why? Probably because the text value of text1 is "" (never set, no call to setText(...) before), so after calling one of the setters, text1s text it is set null or something of the kind. I will try to circumvent the measure(...), but it would be nice to get an explanation.
Thank you!