FragView for CompoundView

时间:2017-01-22 04:33:09

标签: android android-fragments nullpointerexception

我基本上按照教程Creating Compound Views on Android创建了一个带有2个TextViews的复合视图TextViewWithSub。 唯一的主要区别似乎是我在片段而不是活动中使用它,这可能是也可能不是问题的一部分。

除了自定义复合视图外,我可以从片段onCreateView访问所有窗口小部件。如果我没有尝试访问,一切正常,复合视图已正确初始化并显示。

我还检查了我的自定义组件上的initializeViews在我尝试访问它之前执行了,所以我不会解释为什么findViewById返回null。

碎片onCreateView:

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.activity_vector_detail, container, false);

        ((TextView)view.findViewById(R.id.testLbl)).setText("foobar"); // works
        ((TextViewWithSub)view.findViewById(R.id.directionLbl)).setMainText("foobar"); // NullPointerException here

        return view;
    }

activity_vector_detail.xml包含以下小部件:

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/testLbl"/>

<my.namespace.TextViewWithSub
    android:id="@+id/directionLbl"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>

自定义化合物类:

public class TextViewWithSub extends RelativeLayout {

    public TextViewWithSub(Context context) {
        super(context);
    }

    public TextViewWithSub(Context context, AttributeSet attrs) {
        super(context);
        initializeViews(context, attrs);
    }

    public TextViewWithSub(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initializeViews(context, attrs);
    }

    public void setMainText(CharSequence text) {
        ((TextView)this.findViewById(R.id.mainText)).setText(text);
    }

    public void setSubText(CharSequence text) {
        ((TextView)this.findViewById(R.id.subText)).setText(text);
    }

    private void initializeViews(Context context, AttributeSet attrs) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.textview_sub, this);

        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextViewWithSub);

        ((TextView)this.findViewById(R.id.mainText)).setText(a.getText(R.styleable.TextViewWithSub_mainText));
        ((TextView)this.findViewById(R.id.subText)).setText(a.getText(R.styleable.TextViewWithSub_subText));

        a.recycle();
    }
}

textview_sub layout:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/mainText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/subText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</merge>

堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: my.namespace.App, PID: 10667
                  java.lang.NullPointerException: Attempt to invoke virtual method 'void my.namespace.TextViewWithSub.setMainText(java.lang.CharSequence)' on a null object reference
                      at my.namespace.activities.VectorDetail.onCreateView(VectorDetail.java:20)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2184)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
                      at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
                      at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:709)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

我发现了问题,愚蠢的错误,抱歉:

public TextViewWithSub(Context context, AttributeSet attrs) {
    super(context);
    initializeViews(context, attrs);
}

构造函数在调用super时缺少了attrs,这可以正常工作

public TextViewWithSub(Context context, AttributeSet attrs) {
    super(context, attrs);
    initializeViews(context, attrs);
}

虽然我不知道为什么当小部件以任何其他方式工作正常时,这会导致NullPointerException ...