我尝试使用自定义片段时出现NullPointerException

时间:2014-04-24 20:57:54

标签: android android-fragments

我正在尝试在Activity中使用Fragment,我已将此代码用于Fragment

public class MyFragment extends Fragment {
    private TextView txtT1, txtT2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.my_fragment, container, false);

        txtT1 = (TextView) getView().findViewById(R.id.txtT1);
        txtT2 = (TextView) getView().findViewById(R.id.txtT2);

        return view;
    }
}

NullPointerException日志在这些行上完全显示错误

        txtT1 = (TextView) getView().findViewById(R.id.txtT1);
        txtT2 = (TextView) getView().findViewById(R.id.txtT2);

名为my_fragment的xml片段就是这个

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="380dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/background"
        android:layout_centerHorizontal="true"
        android:orientation="vertical"
        android:id="@+id/BoxFragment"
        android:layout_margin="5dp"
        android:layout_gravity="center_horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="@string/title"
            android:gravity="center_horizontal"
            android:id="@+id/txtTitle"
            android:textSize="13sp"/>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/scrollView"
            android:layout_gravity="center_horizontal"
            android:fillViewport="true"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            >

            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/horizontalScrollView"
                android:fillViewport="true">

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center">

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:id="@+id/summaryRow1">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:text="@string/sample1"
                            android:id="@+id/titlT1"
                            android:textSize="13sp"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:text="..."
                            android:id="@+id/txtT1"
                            android:gravity="center_horizontal"
                            android:textSize="12sp"
                            android:layout_marginLeft="3dp"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:text="@string/sample2"
                            android:id="@+id/titlT2"
                            android:textSize="13sp"
                            android:layout_marginLeft="5dp"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:text="..."
                            android:id="@+id/txtT2"
                            android:textSize="12sp"
                            android:layout_marginLeft="3dp"/>

                    </LinearLayout>

                </LinearLayout>
            </HorizontalScrollView>
        </ScrollView>

    </LinearLayout>


</LinearLayout>

1 个答案:

答案 0 :(得分:2)

getView()会返回View中返回的onCreateView(),因此当您在getView()中调用onCreateView()时,它应该为空。

尝试更改此行

    View view = inflater.inflate(R.layout.my_fragment, container, false);

    txtT1 = (TextView) getView().findViewById(R.id.txtT1);
    txtT2 = (TextView) getView().findViewById(R.id.txtT2);

    View view = inflater.inflate(R.layout.my_fragment, container, false);

    txtT1 = (TextView) view.findViewById(R.id.txtT1);
    txtT2 = (TextView) view.findViewById(R.id.txtT2);