1

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 tag 包含的标题布局的高度。

我还向包含的布局(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)
4

1 回答 1

-1

试试这个,因为你的视图已经包含在你的 xml 中。

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

代替

rllayout=(RelativeLayout)includeView.findViewById(R.id.ly_include);
于 2016-06-01T12:23:09.397 回答