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



我想从数据库中动态创建单选按钮,但活动显示错误和崩溃。有一个致命的异常主错误,这里是logcat:

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> 

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

ll.addView(rg);

上面的行采用RRG RadioGroup并将其添加到动态创建的LinearLayout中,但它已经存在于xml布局中定义的Linear布局中。

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

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

那里的错误:

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

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

最新更新