2014-01-22 54 views
0

我从服务器检索信息,并尝试以表格格式显示它,因此使用json对象。“指定的子项已经有父项。您必须先调用子项的父项的removeView()。”异常在android

代码:

JSONArray jsonArray = new JSONArray(jsonResult); 
       TableLayout tv=(TableLayout) findViewById(R.id.table); 
       tv.removeAllViewsInLayout(); 

       int flag=1; 
       for (int i = 0; i < jsonArray.length() ; i++) { 
        //JSONObject object1 = jsonArray.getJSONObject(i); 

         TableRow tr=new TableRow(viewtimetable.this); 

         tr.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT, 
            LayoutParams.WRAP_CONTENT)); 

         if(flag==1) 
         { 

          TextView col1=new TextView(viewtimetable.this); 
          col1.setText("Day"); 
          col1.setTextColor(Color.BLUE); 
          col1.setTextSize(15); 
          tr.addView(col1); 


          TextView col2=new TextView(viewtimetable.this); 
          col2.setPadding(10, 0, 0, 0); 
          col2.setTextSize(15); 
          col2.setText("7:30-9:10AM"); 
          col2.setTextColor(Color.BLUE); 
          tr.addView(col2); 

          TextView col3=new TextView(viewtimetable.this); 
          col3.setPadding(10, 0, 0, 0); 
          col3.setText("9:20-11:00AM"); 
          col3.setTextColor(Color.BLUE); 
          col3.setTextSize(15); 
          tr.addView(col3); 

          TextView col4=new TextView(viewtimetable.this); 
          col4.setPadding(10, 0, 0, 0); 
          col4.setText("11:10-12:50PM"); 
          col4.setTextColor(Color.BLUE); 
          col4.setTextSize(15); 
          tr.addView(col4); 

          TextView col5=new TextView(viewtimetable.this); 
          col5.setPadding(10, 0, 0, 0); 
          col5.setText("1:40-3:20PM"); 
          col5.setTextColor(Color.BLUE); 
          col5.setTextSize(15); 
          tr.addView(col5); 

          TextView col6=new TextView(viewtimetable.this); 
          col6.setPadding(10, 0, 0, 0); 
          col6.setText("3:30-5:00PM"); 
          col6.setTextColor(Color.BLUE); 
          col6.setTextSize(15); 
          tr.addView(col6); 

          tv.addView(tr); 

          final View vline = new View(viewtimetable.this); 

          vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2)); 
          vline.setBackgroundColor(Color.BLUE); 



         tv.addView(vline); 
         flag=0; 


         } 

         else 
         { 

         try{ 

          JSONObject json_data = jsonArray.getJSONObject(i); 

          //Log.i("log_tag","id: "+json_data.getInt("f1")+ 
          //   ", Username: "+json_data.getString("f2")+ 
           //  ", No: "+json_data.getInt("f3")); 


          //((ViewGroup)tr.getParent()).removeView(tr); 

        TextView b=new TextView(viewtimetable.this); 
         String stime=json_data.getString("day"); 
         b.setText(stime); 
         b.setTextColor(Color.RED); 
         b.setTextSize(15); 
         tr.addView(b); 


         TextView b1=new TextView(viewtimetable.this); 
         b1.setPadding(10, 0, 0, 0); 
         b1.setTextSize(15); 
         if(json_data.getString("slot").equals("7:30-9:10AM")){ 
         String stime1=json_data.getString("subject"); 
         b1.setText(stime1); 
         } 
         b1.setTextColor(Color.WHITE); 
         tr.addView(b1); 

         TextView b2=new TextView(viewtimetable.this); 
        b2.setPadding(10, 0, 0, 0); 

         if(json_data.getString("slot").equals("9:20-11:00AM")){ 
          String stime2=json_data.getString("subject"); 
          b2.setText(stime2); 
          } 
         b2.setTextColor(Color.RED); 
         b2.setTextSize(15); 
         tr.addView(b2); 

         TextView b3=new TextView(viewtimetable.this); 
         b3.setPadding(10, 0, 0, 0); 

          if(json_data.getString("slot").equals("11:10-12:50PM")){ 
           String stime3=json_data.getString("subject"); 
           b3.setText(stime3); 
           } 
          b3.setTextColor(Color.RED); 
          b3.setTextSize(15); 
          tr.addView(b2); 

          TextView b4=new TextView(viewtimetable.this); 
          b4.setPadding(10, 0, 0, 0); 

           if(json_data.getString("slot").equals("1:40-3:20PM")){ 
            String stime4=json_data.getString("subject"); 
            b4.setText(stime4); 
            } 
           b4.setTextColor(Color.RED); 
           b4.setTextSize(15); 
           tr.addView(b2);  

           TextView b5=new TextView(viewtimetable.this); 
           b5.setPadding(10, 0, 0, 0); 

           if(json_data.getString("slot").equals("3:30-5:00PM")){ 
            String stime5=json_data.getString("subject"); 
             b5.setText(stime5); 
            } 
           b5.setTextColor(Color.RED); 
           b5.setTextSize(15); 
           tr.addView(b2);   

          tv.addView(tr); 


         }catch(JSONException e){ 
          content.setText("jsonexception"); 
         } 
       final View vline1 = new View(viewtimetable.this); 
       vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1)); 
       vline1.setBackgroundColor(Color.WHITE); 
       tv.addView(vline1);  


         } 

寻找解决的办法,我发现here后,但我不知道在哪里可以使用此line.I不是在获取有关的例外是孩子这将是一个如果你能解释这一点,那么很有帮助

问题: 我知道从上面的链接,下面的行是我的问题的解决方案。

((ViewGroup)scrollChildLayout.getParent()).removeView(scrollChildLayout); 

但在我的情况下,如何以及为什么要使用它?

回答

2

在TextViews b3,b4和b5的代码中,您正在尝试执行tr.addView(b2);,因此b2总共添加了4次。这应该是b3,b4和b5

在旁注中,尝试学习如何阅读崩溃日志。异常应该说明正好错误是在哪行代码上,所以可能在第二次发生时tr.addView(b2);

+0

感谢您计算出我愚蠢而愚蠢的错误。并感谢您的建议。 –

相关问题