2014-09-01 49 views
0

我想從數據庫動態創建單選按鈕,但活動顯示錯誤和崩潰。有致命異常主要錯誤,這裏是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 [email protected] {com.example.ecpvotesystem/com.example.ecpvotesystem.MainActivity}} 
09-01 14:37:55.635: D/ActivityThread(2143): ACT-PAUSE_ACTIVITY handled : 1/[email protected] 
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> 
+0

它不返回rg並提供null。幫我解決這個問題。提前致謝。 – Awais 2014-09-01 09:53:54

回答

1

你嘗試將RadioGroup中添加到您從現有佈局中取出前動態地創建一個新的佈局......

ll.addView(rg); 

上面一行採取RRG RadioGroup中,並把它添加到動態創建了LinearLayout,但它已經存在於xml佈局中定義的LinearLayout中。

在試圖將其添加到新佈局之前,您可以動態創建您的RadioGroup(例如在代碼中)或從其父級中移除它,例如請嘗試以下...

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

我嘗試這個,但應用程序再次崩潰相同的錯誤 – Awais 2014-09-01 12:57:56

+0

我想顯示ll在RRG無線電組的xml – Awais 2014-09-01 13:07:04

+0

我試圖通過創建新的無線電組,它現在工作,由於識別錯誤。感謝幫助 – Awais 2014-09-01 14:04:00

0

的錯誤有:

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

您嘗試添加rgll然後llrg。你想做什麼?

+0

我將rg以GetResultList作爲參數,以便從數據庫信息中創建動態單選按鈕,並將添加到電臺組中,並且函數將返回該電臺組rg。該rg將分配給創建爲ll的線性佈局,然後這將顯示在佈局的RRG中。 – Awais 2014-09-01 13:03:18

+0

我想在RRG廣播組裏顯示ll在xml中 – Awais 2014-09-01 13:07:49

+0

如果你想把'll'放入'RRG'中,試着移除'll.addView(rg);'。 – erakitin 2014-09-01 13:17:18