java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int android.view.View.getVisibility()'

时间:2019-11-27 16:06:06

标签: java android nullpointerexception

我是Android新手,基本上,我是iOS开发人员。

我有一个由其他人实现的android项目,却陷入崩溃。

这是我的TrackerFragment.java代码。

public class TrackerFragment extends Fragment implements View.OnClickListener {

    LinearLayout bloodPressureLayout, viralLoadLayout, cd4Layout, weightLayout;

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

        bloodPressureLayout = (LinearLayout)view.findViewById(R.id.blood_pressure_layout);
        viralLoadLayout = (LinearLayout)view.findViewById(R.id.viral_load_layout);
        cd4Layout = (LinearLayout)view.findViewById(R.id.cd4_layout);
        weightLayout = (LinearLayout)view.findViewById(R.id.weight_layout);

        bloodPressureLayout.setOnClickListener(this);
        viralLoadLayout.setOnClickListener(this);
        cd4Layout.setOnClickListener(this);
        weightLayout.setOnClickListener(this);

        return view;  //Crashing here
    }

这是.XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        style="@style/standard_bold_white_text_clickable"
        android:layout_width="match_parent"
        android:layout_height="@dimen/content_title_height"
        android:background="@color/orange"
        android:gravity="center"
        android:text="@string/tracker" />
    <RelativeLayout
        android:layout_margin="@dimen/fhd_20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TableLayout
            android:id="@+id/tracker_list"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:weightSum="4"
            android:layout_centerHorizontal="true">
            <TableRow
                android:id="@+id/row1"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1">
                <LinearLayout
                    android:id="@+id/blood_pressure_layout"
                    android:layout_width="@dimen/tracker_item_width"
                    android:layout_height="match_parent"
                    android:layout_column="1"
                    android:background="@drawable/box_shadow"
                    android:foreground="?attr/selectableItemBackground"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:padding="@dimen/items_margin">

                    <ImageView
                        android:layout_width="@dimen/image_size"
                        android:layout_height="@dimen/image_size"
                        android:layout_marginBottom="@dimen/home_item_layout_marginBottom"
                        android:src="@drawable/blood_pressure" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/tracker_bp"
                        android:textSize="@dimen/small_font" />
                </LinearLayout>
            </TableRow>
            <TableRow
                android:id="@+id/row2"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1">
                <LinearLayout
                    android:id="@+id/viral_load_layout"
                    android:layout_width="@dimen/tracker_item_width"
                    android:layout_height="match_parent"
                    android:layout_column="1"
                    android:background="@drawable/box_shadow"
                    android:foreground="?attr/selectableItemBackground"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:padding="@dimen/items_margin">

                    <ImageView
                        android:layout_width="@dimen/image_size"
                        android:layout_height="@dimen/image_size"
                        android:layout_marginBottom="@dimen/home_item_layout_marginBottom"
                        android:src="@drawable/viral_load" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/tracker_viral_load"
                        android:textSize="@dimen/small_font" />
                </LinearLayout>
            </TableRow>
            <TableRow
                android:id="@+id/row3"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1">
                <LinearLayout
                    android:id="@+id/cd4_layout"
                    android:layout_width="@dimen/tracker_item_width"
                    android:layout_height="match_parent"
                    android:layout_column="1"
                    android:background="@drawable/box_shadow"
                    android:foreground="?attr/selectableItemBackground"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:padding="@dimen/items_margin">

                    <ImageView
                        android:layout_width="@dimen/image_size"
                        android:layout_height="@dimen/image_size"
                        android:layout_marginBottom="@dimen/home_item_layout_marginBottom"
                        android:src="@drawable/cd4" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/tracker_cd4"
                        android:textSize="@dimen/small_font" />
                </LinearLayout>
            </TableRow>
            <TableRow
                android:id="@+id/row4"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1">
                <LinearLayout
                    android:id="@+id/weight_layout"
                    android:layout_width="@dimen/tracker_item_width"
                    android:layout_height="match_parent"
                    android:layout_column="1"
                    android:background="@drawable/box_shadow"
                    android:foreground="?attr/selectableItemBackground"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:padding="@dimen/items_margin">

                    <ImageView
                        android:layout_width="@dimen/image_size"
                        android:layout_height="@dimen/image_size"
                        android:layout_marginBottom="@dimen/home_item_layout_marginBottom"
                        android:src="@drawable/weight" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/tracker_weight"
                        android:textSize="@dimen/small_font" />
                </LinearLayout>
            </TableRow>
            </TableLayout>
    </RelativeLayout>
</LinearLayout>

以下是崩溃日志:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.android.genvoya, PID: 7745
    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference
        at android.widget.LinearLayout.forceUniformHeight(LinearLayout.java:1409)
        at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1397)
        at android.widget.TableRow.onMeasure(TableRow.java:113)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:461)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.TableLayout.measureVertical(TableLayout.java:473)
        at android.widget.TableLayout.onMeasure(TableLayout.java:436)
        at android.view.View.measure(View.java:18788)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
        at android.view.View.measure(View.java:18788)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographer.doFrame(Choreographer.java:606)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
        at android.os.Handler.handleCallback(Handl

谢谢。

0 个答案:

没有答案