我有一個基於布爾選項創建的視圖,視圖標題爲在單獨的xml文件中定義的socialoptions。當我試圖通過諸如socialoptions.setClickable(true);
等方法使用它時,視圖返回null。Android:查看調用時返回null?
Java代碼:
if (isUserProfile) {
Log.d("user","user");
middlelayout = (LinearLayout) findViewById (R.layout.usermiddlelayout);
} else {
Log.d("item","item");
middlelayout = (LinearLayout) findViewById (R.layout.itemmiddlelayout);
socialoptions = (TextView) findViewById (R.id.socialoptions);
map = (TextView) findViewById (R.id.map);
}
XML代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/itemmiddlelayout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:background="@android:color/transparent"
android:clickable="false"
android:orientation="horizontal" >
<RelativeLayout
android:id="@+id/sociallayout"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="@android:color/transparent"
android:paddingBottom="0dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/socialoptions"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent"
android:clickable="false" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/maplayout"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:paddingBottom="10dp"
android:paddingTop="0dp" >
<TextView
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent"
android:clickable="false" />
</RelativeLayout>
</LinearLayout>
'middlelayout'還有'null'? – Eric
是的,middlelayout也是空的。 – jrquick
那麼我懷疑你沒有從根視圖或者調用'setContentView'的'Activity'引用它。嘗試調用'findViewById(android.R.id.content)'並查看它是否爲'null'。 – Eric