我有level1.xml具有相對佈局和圖像視圖作爲孩子,我想找回這些意見progrmatically這裏設置他們的利潤是我的XML空異常錯誤而孩子的檢索陣列的意見
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Level1Layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="objectClick"
android:background="@drawable/hh_gmw_03">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="593dp"
android:layout_marginLeft="998dp"
android:onClick="objectClick"
android:contentDescription="Image1"
android:tag="Image1"
android:src="@drawable/hht3l01"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="39dp"
android:layout_marginLeft="20dp"
android:onClick="objectClick"
android:contentDescription="Image1"
android:tag="Image1"
android:src="@drawable/hht3l02" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="153dp"
android:layout_marginLeft="497dp"
android:onClick="objectClick"
android:contentDescription="Image1"
android:tag="Image3"
android:src="@drawable/hht3l03" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="524dp"
android:layout_marginLeft="998dp"
android:onClick="objectClick"
android:contentDescription="Image1"
android:tag="Image4"
android:src="@drawable/hht3l04" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="183dp"
android:layout_marginLeft="715dp"
android:onClick="objectClick"
android:contentDescription="Image1"
android:tag="Image5"
android:src="@drawable/hht3l05" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="413dp"
android:layout_marginLeft="557dp"
android:onClick="objectClick"
android:contentDescription="Image1"
android:tag="Image6"
android:src="@drawable/hht3l06" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="53dp"
android:layout_marginLeft="532dp"
android:onClick="objectClick"
android:contentDescription="Image1"
android:tag="Image7"
android:src="@drawable/hht3l07" />
</RelativeLayout>
這裏是我爲ExampleActivity
public class ExampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView[] Objects = (ImageView[]) getViewArrays(R.layout.level1);
Log.d("Check","Objects" + Objects.toString());
}
public View[] getViewArrays(int resid)
{
ViewGroup layout = (ViewGroup) this.findViewById(resid);
View[] views = new View[layout.getChildCount()];
for (int i = 0; i < layout.getChildCount(); i++) {
views[i] = layout.getChildAt(i);
}
return views;
}
public int[] getViewIds(int resid)
{
ViewGroup layout = (ViewGroup) this.findViewById(resid);
int[] ids = new int[layout.getChildCount()];
for (int i = 0; i < layout.getChildCount(); i++) {
ids[i] = layout.getChildAt(i).getId();
}
return ids;
}
}
這是給我空異常錯誤在該行 查看[] =意見新的View [layout.getChildCoun T()]; ......爲什麼我的佈局沒有找到?