0

我动态创建了单选按钮并将它们设置在单选组中,它运行良好,但是当我单击单选按钮以获取值但 onclick 不起作用时,这里是代码:当我单击选中的更改列表时,有什么都没有发生,我也将它显示为 onclick 是否正常工作,但没有打印。请帮助提前谢谢。

   try {
        Thread.sleep(1000);

        voterNA=dbcon.GetVoterNA(id);

        LinearLayout ll=(LinearLayout)findViewById(R.id.NLinearLayout);
        ll.setOrientation(LinearLayout.VERTICAL);
        ImageView iv=null;//new ImageView(context);
        LinearLayout.LayoutParams prams=new RadioGroup.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        prams.setMargins(2, 2, 2, 2);

        rg=new RadioGroup(context);
        ll.setLayoutParams(prams);
        rg.setBackgroundColor(Color.LTGRAY);
        rg.setLayoutParams(prams);

        rg=dbcon.showNationalCan(rg, context,iv,voterNA);

        ll.addView(rg);

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                for(int i=0; i<group.getChildCount();i++)
                {
                    RadioButton rbtn=(RadioButton)group.getChildAt(i);
                    if(rbtn.getId()==checkedId)
                    {
                        serial=rbtn.getId();
                        Toast.makeText(context, serial, 1).show();
                        flag=true;
                        return;
                    }
                    else
                    {
                        Toast.makeText(context, "Else", 1).show();

                        rbtn.setChecked(false);
                    }
                }
            }
        });

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

在上面的代码中,我想获取单选按钮的 id,这里是创建单选按钮的函数。

    public RadioGroup showNationalCan(RadioGroup rg,Context context,ImageView iv,String voterNA){

    //candidate address  as punjab kpk etc

    if(conn==null){

    }
    try{
        RadioButton rb;
        Statement st=conn.createStatement();
        ResultSet rs=st.executeQuery("select * from AM_NATIONAL where ca_na=N'"+voterNA+"'");
        while (rs.next()) {
            rb=new RadioButton(context);
            rb.setTextColor(Color.BLACK);
            rb.setId(rs.getInt(1));
            rb.setText(rs.getString(2)+"\n"+rs.getString(3)+"\n");

            iv=new ImageView(context);
            byte[] photo=rs.getBytes(4);
            Bitmap bitmap;
            bitmap=BitmapFactory.decodeByteArray(photo, 0, photo.length);
            iv.setImageBitmap(bitmap);

            //rb.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.blank, 0, 0, 0);
            rg.addView(iv);
            rg.addView(rb);
        }
        rs.close();
        st.close();

    } catch (SQLException e){
        e.printStackTrace();
    }
    return rg;
}

setid() 和 settext() 可以分别调用吗?

这是xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/background3"
tools:context=".NationalActivity" >

<ScrollView
    android:id="@+id/NscrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:id="@+id/NLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10sp"
        android:orientation="vertical" >


        <ImageView
            android:id="@+id/Nokimagebtn"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:src="@drawable/necpbin" />

        <ImageView
            android:id="@+id/Nbackimagebtn"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:src="@drawable/back" />

        <ImageView
            android:id="@+id/Nexitimagebtn"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:src="@drawable/exit" />

    </LinearLayout>
</ScrollView>

</RelativeLayout>

这是更新代码后应用程序崩溃时的logcat:

09-02 17:31:11.769: W/dalvikvm(31156): threadid=1: thread exiting with uncaught exception (group=0x40ff2908)
09-02 17:31:11.789: E/AndroidRuntime(31156): FATAL EXCEPTION: main
09-02 17:31:11.789: E/AndroidRuntime(31156): java.lang.ClassCastException: android.widget.RadioGroup$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:311)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.widget.ScrollView.onMeasure(ScrollView.java:318)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.View.measure(View.java:15264)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.View.measure(View.java:15264)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.View.measure(View.java:15264)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.View.measure(View.java:15264)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2161)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.View.measure(View.java:15264)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2129)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1240)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1433)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1125)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4607)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:747)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.Choreographer.doCallbacks(Choreographer.java:567)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.Choreographer.doFrame(Choreographer.java:536)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:733)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.os.Handler.handleCallback(Handler.java:615)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.os.Looper.loop(Looper.java:153)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at android.app.ActivityThread.main(ActivityThread.java:5086)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at java.lang.reflect.Method.invokeNative(Native Method)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at java.lang.reflect.Method.invoke(Method.java:511)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-02 17:31:11.789: E/AndroidRuntime(31156):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

你犯了一个小错误。

rg您已经使用 java 代码动态添加了一个单选组。

然后你添加它并以错误的方式使用它。

代替

rg=dbcon.showNationalCan(rg, context,iv,voterNA);
((ViewGroup)findViewById(R.id.NRG)).addView(rg);
final RadioGroup rg1=(RadioGroup)findViewById(R.id.NRG);

rg=dbcon.showNationalCan(rg, context,iv,voterNA);
ll.addView(rg);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

希望这可以帮助。

于 2014-09-02T11:38:02.703 回答
0

如果对任何人有帮助,我已经根据我的要求解决了这个问题

 RadioGroup ll = new RadioGroup(this);
 ll.setOrientation(LinearLayout.VERTICAL);

 for (int i = 0; i < 5; i++) 
 {
     RadioButton rdbtn = new RadioButton(this);
     rdbtn.setId(i);
     rdbtn.setTag("your_tag");
     rdbtn.setText("radio_btn_name");
     ll.addView(rdbtn);
 }

 ll.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
 {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) 
        {
            Log.e("conductor_radio_chnged", checkedId+"  =");
        }
 });

 ((ViewGroup)findViewById(R.id.radiogroup)).addView(ll);
于 2017-07-28T14:44:58.840 回答