0
我正在處理一個android應用程序,我有這個問題。我想在一個滾動視圖中動態地添加複選框,它將顯示在對話框中。 問題是通過此表達式時佈局爲null ScrollView layout = (ScrollView) findViewById(R.layout.custom);
FindViewById返回NULL滾動查看
這裏是函數。
private void openCustomMenu() {
// TODO Auto-generated method stub
ScrollView layout = (ScrollView) findViewById(R.layout.custom);
layout.removeAllViews();
for (String name : StopNames){
CheckBox checkbox = new CheckBox(this);
checkbox.setText(name);
checkbox.setTextColor(getResources().getColor(R.color.white));
layout.addView(checkbox);
}
final Dialog dialog = new Dialog(this);
dialog.setContentView(layout);
dialog.setTitle("Bus Stops");
}
這裏是我的xml文件。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</ScrollView>
Thank you for you help !
編輯:新的XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/custom">
</LinearLayout>
</ScrollView>
和新功能。
private void openCustomMenu() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Bus Stops");
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.custom);
layout.removeAllViews();
for (String name : StopNames){
CheckBox checkbox = new CheckBox(this);
checkbox.setText(name);
checkbox.setTextColor(getResources().getColor(R.color.white));
layout.addView(checkbox);
}
}
謝謝!現在我沒有收到任何錯誤,但對話窗口根本沒有出現。我改變了一點點我的XML。 – nytochin
您沒有在代碼中將'R.layout.custom'更改爲'findViewById(R.id.custom);'。如果這個答案回答你的問題,你應該接受它。然後,如果您有其他問題,則必須提出另一個問題。 –