ViewTreeObserver为getMeasuredHeight()方法提供空指针异常

时间:2016-06-01 12:08:15

标签: android android-viewtreeobserver

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

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.activity.Example">

        <include android:id="@+id/include2"
            layout="@layout/header_layout" />

        <LinearLayout
            android:id="@+id/help_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/help_text_background"
            android:visibility="gone"
            android:gravity="center">

            <TextView
                android:id="@+id/help_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:textColor="@color/help_text_color"/>

        </LinearLayout>

        <include android:id="@+id/include1"
            layout="@layout/terms_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/include2"/>


    </LinearLayout>



    <LinearLayout
        android:id="@+id/nav_main_height"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="@color/white">

        <include
            android:id="@+id/navigationdrawer"
            layout="@layout/drawer_menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

在这里,我想获得使用include标记包含的标题布局的高度。

此外,我还为包含的布局(header_layout)提供了ID,其ID为 ly_include 。我使用下面的代码来测量布局的高度,但它给出了上面提到的错误。

我宣布了包含视图并初始化如下

View includeView;
RelativeLayout rllayout;
includeView=findViewById(R.id.include2);
rllayout=(RelativeLayout)includeView.findViewById(R.id.ly_include);

final int[] height = new int[1];

下面是ViewTreeObserver代码

ViewTreeObserver vto = rllayout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    rllayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
                int width  = rllayout.getMeasuredWidth();
                height[0] = rllayout.getMeasuredHeight();

            }
        });

下面是日志

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.example, PID: 24849
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.activity.Example}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewTreeObserver android.view.View.getViewTreeObserver()' on a null object reference
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewTreeObserver android.view.View.getViewTreeObserver()' on a null object reference
                                                                            at com.example.activity.Example.onCreate(Example.java:82)

1 个答案:

答案 0 :(得分:-1)

尝试此,因为您的视图已包含在xml中。

rllayout=(RelativeLayout)findViewById(R.id.ly_include);

而不是

rllayout=(RelativeLayout)includeView.findViewById(R.id.ly_include);