Java 动态单选按钮中出现致命异常错误

Java 动态单选按钮中出现致命异常错误,java,android,android-layout,android-view,Java,Android,Android Layout,Android View,我想从数据库动态创建单选按钮,但活动显示错误和崩溃。存在致命异常主错误,以下是日志: 09-01 14:37:54.628: I/SurfaceTextureClient(2143): [0x40f0a7d8] frames:54, duration:1.018000, fps:53.007999 09-01 14:37:55.612: D/dalvikvm(2143): threadid=12: exiting 09-01 14:37:55.612: D/dalvikvm(2143): thr

我想从数据库动态创建单选按钮,但活动显示错误和崩溃。存在致命异常主错误,以下是日志:

09-01 14:37:54.628: I/SurfaceTextureClient(2143): [0x40f0a7d8] frames:54, duration:1.018000, fps:53.007999
09-01 14:37:55.612: D/dalvikvm(2143): threadid=12: exiting
09-01 14:37:55.612: D/dalvikvm(2143): threadid=12: bye!
09-01 14:37:55.615: D/ActivityThread(2143): ACT-AM_ON_PAUSE_CALLED ActivityRecord{418026e8 token=android.os.BinderProxy@41801eb {com.example.ecpvotesystem/com.example.ecpvotesystem.MainActivity}}
09-01 14:37:55.635: D/ActivityThread(2143): ACT-PAUSE_ACTIVITY handled : 1 / android.os.BinderProxy@41801eb8
09-01 14:37:55.693: D/dalvikvm(2143): GC_FOR_ALLOC freed 375K, 4% free 16746K/17271K, paused 18ms, total 23ms
09-01 14:37:55.979: D/AndroidRuntime(2143): Shutting down VM
09-01 14:37:55.979: W/dalvikvm(2143): threadid=1: thread exiting with uncaught exception (group=0x40ff2908)
09-01 14:37:55.999: E/AndroidRuntime(2143): FATAL EXCEPTION: main
09-01 14:37:55.999: E/AndroidRuntime(2143): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ecpvotesystem/com.example.ecpvotesystem.ResultActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.app.ActivityThread.access$600(ActivityThread.java:149)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.os.Looper.loop(Looper.java:153)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.app.ActivityThread.main(ActivityThread.java:5086)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at java.lang.reflect.Method.invoke(Method.java:511)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at dalvik.system.NativeStart.main(Native Method)
09-01 14:37:55.999: E/AndroidRuntime(2143): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3459)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.view.ViewGroup.addView(ViewGroup.java:3330)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.view.ViewGroup.addView(ViewGroup.java:3275)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.view.ViewGroup.addView(ViewGroup.java:3251)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at com.example.ecpvotesystem.ResultActivity.onCreate(ResultActivity.java:52)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.app.Activity.performCreate(Activity.java:5020)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-01 14:37:55.999: E/AndroidRuntime(2143):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
09-01 14:37:55.999: E/AndroidRuntime(2143):     ... 11 more
下面是java文件的代码:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

dbcon=DBConnection.instance(this);
dbcon.connect("ip:1433", "pas", "login", "db");
Context cntxt=getApplicationContext();

try {

    Thread.sleep(10);

    String city=getIntent().getExtras().getString("city");
    String choice=getIntent().getExtras().getString("choice");

    LinearLayout ll=new LinearLayout(cntxt);
    ll.setOrientation(LinearLayout.VERTICAL);
    ImageView iv=new ImageView(cntxt);
    RadioGroup.LayoutParams prams=new RadioGroup.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
    prams.setMargins(0, 2, 0, 2);


    RadioGroup rg=(RadioGroup)findViewById(R.id.RRG);

        rg=dbcon.GetResultList(rg,choice,city,iv,cntxt);

        ll.addView(rg);

        ((ViewGroup)findViewById(R.id.RRG)).addView(ll);



} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}
下面是GetResultList()函数,其中包含针对它调用的参数:

@SuppressLint("NewApi")
public LinearLayout GetResultList(RadioGroup rg,String choice,String city,ImageView iv,Context context) {
// TODO Auto-generated method stub

if(conn==null){

}
try{
    RadioButton rb;
    rg.setBackgroundColor(Color.LTGRAY);

        Statement st=conn.createStatement();
        ResultSet rs=st.executeQuery("select * from AM_NATIONAL where ca_city="+city+ "order by cvg_count");

        while (rs.next()){
            rb=new RadioButton(context);
            rb.setId(rs.getInt(1));
            rb.setText(rs.getString(2)+"\n");
            rb.setText(rs.getString(3)+"\n");
            rb.setText(rs.getInt(8));

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

            rb.setEnabled(false);
            rg.addView(iv);
            rg.addView(rb);


        }
        rs.close();
        st.close();
    }
    catch(SQLException e){

    }
   return rg;

   }
下面是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"
 tools:context=".ResultActivity" >

 <ScrollView
     android:id="@+id/scrollView1"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_below="@+id/imageView1"
     android:layout_alignParentLeft="true" >

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical" >

         <RadioGroup 
             android:id="@id/RRG"
             android:layout_width="match_parent"
             android:layout_height="wrap_content" >

         </RadioGroup>

     </LinearLayout>
 </ScrollView>


 </RelativeLayout> 

此处的错误:

ll.addView(rg);
((ViewGroup)findViewById(R.id.RRG)).addView(ll);

您正在尝试将
rg
添加到
ll
中,然后将
ll
添加到
rg
中。您想做什么?

您正在尝试将您的RadioGroup添加到动态创建的新布局中,然后将其从现有布局中删除

ll.addView(rg);
上面的行获取RRG RadioGroup并将其添加到动态创建的LinearLayout中,但它已经存在于xml布局中定义的LinearLayout中

动态创建您的RadioGroup(例如,在代码中)或在尝试将其添加到新布局之前将其从其父级删除,例如,尝试以下操作

((LinearLayout)rg.getParent()).removeView(rg);
ll.addView(rg);

它不会返回rg并提供null。帮我解决这个问题。提前感谢。我将rg作为参数赋给GetResultList,以便从数据库信息创建动态单选按钮,该按钮将添加到单选组,函数将返回该单选组rg。该rg将分配给创建为ll的线性布局,然后这将显示在布局的RRG中。我想在XML中的RRG无线电组中显示ll如果您想将
ll
放入
RRG
中,请尝试删除
ll.addView(rg)。我想在xml的RRG无线电组中显示ll。我通过创建新的无线电组尝试了这一点,现在由于识别了错误,它正在工作。谢谢你的帮助