我从这里按照教程添加行,但我遇到了问题,当我尝试添加视图时遇到问题......这是我的代码:
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)
这是我的布局:
我不知道我的错在哪里..我希望有人可以帮助我解决我的问题。