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

我在第335行尝试输入NullPointerException时得到一个setText(...),尽管我之前特别检查了它是否不为null:

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

}


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

更新3:

问题似乎是:text1.measure(0,textHeight)
尽管textHeight=0没关系,但我尝试使用其他值。

它似乎也只有在其他设置器之后调用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(...)),所以在调用设置器之一,text1的文本之后,将其设置为null或类似的类型。我将尝试绕过measure(...),但是得到解释会很好。
谢谢!

最佳答案

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

指出问题所在的地方是textView没有layoutParams:

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

关于android - View 在setText上为null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24908406/

10-12 00:39