ViewTreeObserver Предоставление исключения нулевого указателя для метода getMeasuredHeight()
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), а его идентификатор - 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 ответ
Попробуйте это, так как ваш взгляд уже включен в ваш XML.
rllayout=(RelativeLayout)findViewById(R.id.ly_include);
вместо
rllayout=(RelativeLayout)includeView.findViewById(R.id.ly_include);