Android,在for循环中添加单选按钮

时间:2011-05-31 18:06:46

标签: java android view for-loop radio-button

我目前正在开发Android 1.5应用 我问我的sqlite数据库中的查询的所有结果,并且我希望每个结果都将RadioButton添加到RadioGroup

private void prepFilialen(){
    List<Filiaal> lstFilialen = this.dh.getAllFilialen();
    RadioGroup rdgrpFilialen = (RadioGroup)findViewById(R.id.radioGroup1);
    for(int i = 0;i<=lstFilialen.size()-1;i++){
        RadioButton rdbtn = new RadioButton(this);
        rdgrpFilialen.addView(rdbtn);
        rdbtn.setId(i);
        rdbtn.setText(lstFilialen.get(i).getFil_Naam());
    }
}

这会导致应用崩溃。我想向RadioButtons添加文字并为其添加ID,并进一步处理所选RadioButton的ID。

这可能吗? 使用Spinner ( = ComboBox)代替RadioButtons时,这可能更简单吗?

DDMS错误报告:

05-31 18:03:47.345: ERROR/AndroidRuntime(1040): Uncaught handler: thread main exiting due to uncaught exception
05-31 18:03:47.375: ERROR/AndroidRuntime(1040): java.lang.NullPointerException
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at com.totsp.androidexamples.Main.prepFilialen(Main.java:66)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at com.totsp.androidexamples.Main.access$1(Main.java:61)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at com.totsp.androidexamples.Main$2.onClick(Main.java:54)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at android.view.View.performClick(View.java:2179)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at android.view.View.onTouchEvent(View.java:3828)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at android.widget.TextView.onTouchEvent(TextView.java:6291)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at android.view.View.dispatchTouchEvent(View.java:3368)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1707)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1197)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at android.app.Activity.dispatchTouchEvent(Activity.java:1993)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1525)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at android.os.Looper.loop(Looper.java:123)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at android.app.ActivityThread.main(ActivityThread.java:3948)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at java.lang.reflect.Method.invokeNative(Native Method)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at java.lang.reflect.Method.invoke(Method.java:521)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
05-31 18:03:47.375: ERROR/AndroidRuntime(1040):     at dalvik.system.NativeStart.main(Native Method)

private void prepFilialen(){
    List<Filiaal> lstFilialen = this.dh.getAllFilialen();
    RadioGroup rdgrpFilialen = (RadioGroup)findViewById(R.id.radioGroup1);

    for(int i = 0;i<=lstFilialen.size()-1;i++){

        RadioButton rdbtn = new RadioButton(this);

        rdgrpFilialen.addView(rdbtn);

        rdbtn.setId(i);

        rdbtn.setText(lstFilialen.get(i).getFil_Naam());
    }
}

这可能是使用spinner代替radiobuttons吗?

1 个答案:

答案 0 :(得分:0)

    private void prepFilialen(){
List<Filiaal> lstFilialen = this.dh.getAllFilialen();
RadioGroup rdgrpFilialen = (RadioGroup)findViewById(R.id.radioGroup1);
RadioButton rdbtn;
for(int i = 0;i<=lstFilialen.size()-1;i++){
    rdbtn = new RadioButton(this);
    rdgrpFilialen.addView(rdbtn);
    rdbtn.setId(i);
}

}

试试吧。让我知道你发布的那些行号是什么。这对我有很大的帮助。我使用EditTexts做了类似的事情并且遇到了类似的问题。