视图在setText上变为null

时间:2014-07-23 10:48:19

标签: android

我有一个自定义ViewGroup,它在View构造函数调用的TextView text1方法中实例化私有init()

我在第335行尝试NullPointerException时得到setText(...),即使我特别检查它之前是否为空:

//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
...
}

这里是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

任何想法可能是什么原因?

UPDATE1:

textWidth=500;
textHeight=0;
textPadding=10;
fontSize=30;

在设置setText()之前检查所有变量和值,而不是null并返回正确的值

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);

}

我删除了尽可能多的代码进行测试。问题依然存在。

UPDATE3:

问题似乎是:text1.measure(0,textHeight)。 虽然textHeight=0虽然没有关系,但我尝试了不同的价值观。

似乎只有在其他setter之后调用text1.measure(...)时才会发生:

// 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);

有人可以解释原因吗?可能是因为text1的文本值是""(从未设置过,之前没有调用setText(...)),所以在调用其中一个setter后,text1的文本被设置了null或类似的东西。我会试图绕过measure(...),但得到一个解释会很好。 谢谢!

1 个答案:

答案 0 :(得分:1)

问题不在setText()中,而是在函数checkForRelayout的Android源代码中,所以我的猜测是答案在于NullPointerException at TextView.checkForRelayout() while setText()

表示问题是textView没有layoutParams:

vh.tv.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));