0

我从这里按照教程添加行,但我遇到了问题,当我尝试添加视图时遇到问题......这是我的代码:

add_sumber_pp=(ImageView)findViewById(R.id.add_sumber_pp);

add_sumber_pp.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
        TableLayout tablelayout_penghasilan_pp=(TableLayout)findViewById(R.id.tablelayout_penghasilan_pp);
        TableRow tabel_penghasilan_pp=(TableRow)findViewById(R.id.tabel_penghasilan_pp);
        counter++;
        TextView adapter_no_dana=(TextView)findViewById(R.id.adapter_no_dana);
        TextView adapter_jenis_dana=(TextView)findViewById(R.id.adapter_jenis_dana);
        TextView adapter_jelaskan_dana=(TextView)findViewById(R.id.adapter_jelaskan_dana);
                ImageView add_pp=(ImageView)findViewById(R.id.add_pp);
                ImageView remove_pp=(ImageView)findViewById(R.id.remove_pp);
                adapter_no_dana.setText("anak "+counter);
                if(v != null) 
                { 
                     ViewGroup parentViewGroup = (ViewGroup) v.getParent();
                        if (parentViewGroup != null) {
                            parentViewGroup.removeAllViews();
                        }

                }
                tabel_penghasilan_pp.addView(adapter_no_dana);
                tabel_penghasilan_pp.addView(adapter_jenis_dana);
                tabel_penghasilan_pp.addView(adapter_jelaskan_dana);
                tabel_penghasilan_pp.addView(add_pp);
                tabel_penghasilan_pp.addView(remove_pp);

            }
        });

这是我的日志:

11-11 11:39:29.944: E/AndroidRuntime(19016): FATAL EXCEPTION: main
11-11 11:39:29.944: E/AndroidRuntime(19016): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
11-11 11:39:29.944: E/AndroidRuntime(19016):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3389)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at android.view.ViewGroup.addView(ViewGroup.java:3260)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at android.view.ViewGroup.addView(ViewGroup.java:3205)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at android.view.ViewGroup.addView(ViewGroup.java:3181)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at id.co.ajsmsig.formspaj.Pemegang_polis$6.onClick(Pemegang_polis.java:308)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at android.view.View.performClick(View.java:4222)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at android.view.View$PerformClick.run(View.java:17337)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at android.os.Handler.handleCallback(Handler.java:615)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at android.os.Looper.loop(Looper.java:137)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at android.app.ActivityThread.main(ActivityThread.java:4895)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at java.lang.reflect.Method.invokeNative(Native Method)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at java.lang.reflect.Method.invoke(Method.java:511)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
11-11 11:39:29.944: E/AndroidRuntime(19016):    at dalvik.system.NativeStart.main(Native Method)

这是我的布局: 在此处输入图像描述

我不知道我的错在哪里..我希望有人可以帮助我解决我的问题。

4

2 回答 2

0

对不起,这些观点

 TextView adapter_no_dana=(TextView)findViewById(R.id.adapter_no_dana);
        TextView adapter_jenis_dana=(TextView)findViewById(R.id.adapter_jenis_dana);
        TextView adapter_jelaskan_dana=(TextView)findViewById(R.id.adapter_jelaskan_dana);

是另一个布局/父级的子级,这就是问题所在。

如果要动态添加它们,则需要进行更改

TextView adapter_no_dana=new TestView(context);
        TextView adapter_jenis_dana=new TestView(context);
        TextView adapter_jelaskan_dana=new TestView(context);
于 2013-11-11T04:53:32.820 回答
0

在更新视图之前添加以下行

tablelayout_penghasilan_pp.removeAllViews();

是的,如果你想动态添加,你应该在你的代码中更新它

TextView adapter_no_dana=new TestView(context);
TextView adapter_jenis_dana=new TestView(context);
TextView adapter_jelaskan_dana=new TestView(context);

希望它会帮助你。

于 2013-11-11T04:58:07.110 回答