我要動態地添加複選框,我的活動佈局,該佈局的XML如下: `如何將CheckBox動態添加到Android佈局?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentSV"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/parentLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<RelativeLayout
android:id="@+id/feedbackRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/feedbackCustomerNameLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Name : "
android:textColor="@android:color/black" />
<TextView
android:id="@+id/feedbackCustomerName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textColor="@android:color/black" />
</LinearLayout>
<LinearLayout
android:id="@+id/feedbackPlansLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/feedbackCustomerNameLL"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Plans Explained"
android:textColor="@android:color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/feedbackPlansCheckBoxLL"
android:orientation="horizontal" >
<!--
Required to Add CheckBoxes Here Dynamically
-->
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
`
複選框都在評論區以復加。如何動態添加它們,因爲我必須根據從服務器發送的數據在運行時添加它們。我無法從層次結構中刪除ScrollView或任何其他視圖。
我不喜歡這樣,但我得到空指針異常的'linear.addView(CB);' – Rahul
你叫的LinearLayout線性=(LinearLayout中)findViewById('your id');在添加複選框之前?你也可以在你添加的地方發佈代碼嗎? –
並且從佈局中可以看到,沒有標識已分配給要添加複選框的線性佈局。所以可能的機會是你可能正在訪問別的東西。也檢查一下。 –