您好我已編碼以下從包含佈局獲取視圖。但我得到空指針異常。我錯在哪裏?如何引用視圖內包含佈局android
我的Java代碼:
View actionbar = findViewById(R.id.actionbar);
RelativeLayout actionRRl = (RelativeLayout)actionbar.findViewById(R.id.actionRR);
actionRRl.setEnabled(false);
actionRRl.setClickable(false);
在我Activity.xml,
<include
android:id="@+id/actionbar"
layout="@layout/actionbar" />
佈局所包含,即actionbar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/header"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:id="@+id/actionRR"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:clickable="false"
android:padding="@dimen/logo_size"
android:src="@drawable/logo" />
</RelativeLayout>
錯誤堆棧:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setEnabled(boolean)' on a null object reference
想你的代碼:它投擲的錯誤隊友! 引起:java.lang.NullPointerException:嘗試調用null對象的虛擬方法'void android.widget.RelativeLayout.setEnabled(boolean)'引用 –
請確保您在'setContentView() –