Radiobutton dynamically
I have a edittext depending on the number given in edit text i have to create a radiobutton but im getting error this is my code
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// ToDo add your GUI initialization code here
setContentView(R.layout.main);
go=(Button)findViewById(R.id.go);
no=(EditText)findViewById(R.id.noofradio);
err=(TextView)findViewById(R.id.err);
go.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
try
{
int a = Integer.parseInt(no.getText().toString());
RadioGroup radiogroup = new RadioGroup(MainActivity.this);
LinearLayout.LayoutParams rg = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < a; i++) {
radiobutton = new RadioButton(MainActivity.this);
radiobutton.setText(i);
radiobutton.setId(i);
radiogroup.addView(radiobutton, rg);
}
}
catch(Exception ex)
{
err.setText(ex.toString());
}
}
});
}
}
this is my xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>"
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButtons"
android:id="@+id/err"/>
<EditText
开发者_运维技巧 android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/noofradio">
</EditText>
<Button
android:id="@+id/go"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Buttons">
</Button>
</LinearLayout>
android content resource$not found exception stringresource id
I think you have an error in your xml (well, using undefined string resource, I guess)
The call to setText() is expecting a string resource id. Specifically, radiobutton.setText(i)
is interpreting 'i' to be a string resource id vs. the index you are passing in.
精彩评论