2013-03-16 167 views
3

Iam開發應用程序以將動態數據添加到多個表視圖。結構如下。問題:指定的孩子已經有父母。您必須先調用孩子父母的removeView()

enter image description here 我在onPostExecute代碼:

protected void onPostExecute(List<String> results){ 
int ind,i = 0; 
ScrollView sv=null; 
TableLayout tl=null; 
TableRow tr = null; 
int mjsonlength=results.size(); 
if(results!=null){ 

TableLayout indextl=(TableLayout)findViewById(R.id.indextablelayout); 
    TableRow indextr=new TableRow(SubProducts.this); 
    //indext1.moveToFirst(); 
    LinearLayout llt=(LinearLayout)findViewById(R.id.alltablell); 
    //for(int f=0;f<mjsonlength;f++){ 

     for(ind=0;ind<indextitle.size();ind++) 
     { 

      TextView indextv=new TextView(SubProducts.this); 
      indextv.setBackgroundResource(R.drawable.greenbg); 
      indextv.setHeight(25); 
      indextv.setWidth(50); 
      indextv.setId(6); 
      indextv.setTextColor(Color.WHITE); 
      indextv.setText(indextitle.get(ind)); 
      indextv.setPadding(20,10,6,3); 
      indextr.addView(indextv); 

      indextl.addView(indextr,6); 

      sv=new ScrollView(SubProducts.this); 
      tl=new TableLayout(SubProducts.this); 
      for(i=0;i<mjsonlength;i++){ 
        tr=new TableRow(SubProducts.this); 

        if(ind==i) { 

         RelativeLayout rl=new RelativeLayout(SubProducts.this); 
         Resources res = getResources(); //resource handle 
          rl.setBackgroundResource(R.drawable.prd_box); 

         RelativeLayout.LayoutParams newParams1 = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, 
            RelativeLayout.LayoutParams.WRAP_CONTENT); 

          image=new ImageView(SubProducts.this); 
          image.setLayoutParams(newParams1); 
          image.setId(1); 
          image.setImageBitmap(bmp); 
          image.setPadding(5, 20, 5, 5); 
          rl.addView(image,1); 



          RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, 
            RelativeLayout.LayoutParams.WRAP_CONTENT); 
          lprams.addRule(RelativeLayout.RIGHT_OF, 1); 

          TextView tv1=new TextView(SubProducts.this); 
         Spanned marked_up = Html.fromHtml(description.get(i)); 
         tv1.setText((marked_up.toString()).substring(1, 170).replaceAll("&nbsp;","")); 
         tv1.setLayoutParams(lprams); 
         tv1.setId(2); 
         tv1.setHeight(150); 
         tv1.setWidth(130); 
         tv1.setPadding(20, 10, 0, 10); 
         rl.addView(tv1,2); 


         RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, 
            RelativeLayout.LayoutParams.WRAP_CONTENT); 
         newParams.addRule(RelativeLayout.BELOW, 2); 
         TextView tv=new TextView(SubProducts.this); 
         tv.setText(productname.get(i)); 
         tv.setLayoutParams(newParams); 
         tv.setTextColor(Color.WHITE); 
         tv.setId(3); 
         tv.setPadding(10, 0, 0, 10); 
         rl.addView(tv,3); 


         tr.addView(rl); 
         tr.setPadding(5, 10, 5, 10); 


        } 
        tl.addView(tr); 

      } 

      sv.addView(tl); 

       llt.addView(sv); 

     } 

    } 

}

蔭收到錯誤如下:

 03-16 06:27:07.452: E/AndroidRuntime(7233): FATAL EXCEPTION: main 
     03-16 06:27:07.452: E/AndroidRuntime(7233): java.lang.IllegalStateException: The 03-16 06:27:07.452: E/AndroidRuntime(7233): FATAL EXCEPTION: main 
03-16 06:27:07.452: E/AndroidRuntime(7233): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at android.view.ViewGroup.addViewInner(ViewGroup.java:3339) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at android.view.ViewGroup.addView(ViewGroup.java:3210) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at android.widget.TableLayout.addView(TableLayout.java:429) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at android.view.ViewGroup.addView(ViewGroup.java:3155) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at android.widget.TableLayout.addView(TableLayout.java:411) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at android.view.ViewGroup.addView(ViewGroup.java:3131) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at android.widget.TableLayout.addView(TableLayout.java:402) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at com.example.nutritionplusapp.SubProducts$LongRunningGetIO1.onPostExecute(SubProducts.java:227) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at com.example.nutritionplusapp.SubProducts$LongRunningGetIO1.onPostExecute(SubProducts.java:1) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at android.os.AsyncTask.finish(AsyncTask.java:631) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at android.os.AsyncTask.access$600(AsyncTask.java:177) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at android.os.Looper.loop(Looper.java:137) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
03-16 06:27:07.452: E/AndroidRuntime(7233):  at dalvik.system.NativeStart.main(Native Method) 
03-16 06:32:07.586: I/Process(7233): Sending signal. PID: 7233 SIG: 9 
03-16 06:32:08.522: E/Trace(7355): error opening trace file: No such file or directory (2) 

循環變量的值應該是相同的兩個循環中序得子產品各自的產品。伊姆與上述錯誤戰鬥。任何人有idea.plz建議我適當的解決方案。在預先感謝。

編輯:

ArrayList<String> productname=new ArrayList<String>(); 
ArrayList<String> description=new ArrayList<String>(); 
ArrayList<String> indextitle=new ArrayList<String>(); 
ArrayList<String> mStrings = new ArrayList<String>(); 

下doINBackground:

JSONObject json = array.getJSONObject(i); 

    manJson = json.getJSONObject("product"); 
    mStrings.add(manJson.getString("productimage")); 
    productname.add(manJson.getString("productname")); 
    indextitle.add(manJson.getString("subcategoryid")); 
    description.add(manJson.getString("description")); 
+0

u能張貼在for循環 – Dinesh 2013-03-16 06:14:13

+0

Thanq Dinesh.I已經編輯我的問題PLZ看看declation和indextitle收集的定義。 – Neeha 2013-03-16 06:18:26

+0

你可以發佈你的json字符串格式嗎? – Dinesh 2013-03-16 06:24:01

回答

12

第二電位問題也: indextl.addView(indextr);

indextr被添加到每一個循環迭代中,但它是相同的視圖。您只能將一個視圖添加到層​​次結構中。您可能需要爲每個循環迭代創建一個新實例。我不能肯定地說,但這可能是問題所在。

+0

thanq James Baca它解決了我的問題。但它只顯示了一個產品,我的意思是它只顯示了product1子產品。我的循環中出現任何問題。如果你有任何想法,PLZ會經歷它。 – Neeha 2013-03-16 07:10:10

+0

我的猜測是這樣的:if(ind == i)這隻能發生一次子產品... – 2013-03-16 07:11:20

+0

你能否建議我使用任何替代邏輯而不是那個。 – Neeha 2013-03-16 07:13:10

2

例外實際上看起來像它的原因是:

indextl.addView(indextr,6);

這個問題似乎您試圖在第6個索引添加視圖,但沒有任何迭代MS本:

java.lang.IndexOutOfBoundsException:   index=6 count=0 
+0

thanq James Baca.I已經解決了它。但是現在Iam得到另一個錯誤,即03-16 06:27:07.452:E/AndroidRuntime(7233):java .lang.IllegalStateException:指定的子項已經有父項。您必須先調用子對象的父對象的removeView()。 – Neeha 2013-03-16 06:28:47

+0

你能發佈更新的堆棧跟蹤嗎? – 2013-03-16 06:34:41

+0

James Baca我已經更新了我的日誌文件有問題本身有一個look.thanq – Neeha 2013-03-16 06:39:41

6

試試這個....

ViewGroup parent = (ViewGroup) mViewParent.getParent(); 
int index = parent.indexOfChild(mViewParent); 
parent.removeView(mViewParent); 

parent.addView(mNewView_U_want_to_add); 
3

在用的onCreate活動或在onCreateView與片段。

if (view != null) { 
ViewGroup parent = (ViewGroup) view.getParent(); 
if (parent != null) { 
    parent.removeView(view); 
} 
}try { 
view = inflater.inflate(R.layout.fragment_main, container, false); 
} catch (InflateException e) {} 
相關問題