gpt4 book ai didi

java - 动态单选按钮中的致命异常错误

转载 作者:行者123 更新时间:2023-12-01 12:35:50 26 4
gpt4 key购买 nike

我想从数据库动态创建单选按钮,但 Activity 显示错误并崩溃。有致命异常主要错误,这是 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 布局中定义的 LinearLayout 中。

要么动态创建RadioGroup(例如在代码中),要么在尝试将其添加到新布局(例如)之前将其从其父级中删除。尝试以下...

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

关于java - 动态单选按钮中的致命异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25603090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com