指定的子级已有父级错误

时间:2014-02-23 04:47:00

标签: android android-tablelayout

此程序显示错误“指定的子项已经有父项。您必须首先在子项的父项上调用removeView()。” 该程序具有EditText(edText),它要求用户输入。 它的类型是NUMBER。(这里r是来自edText的值) 在EditText下面有一个Button(bt),它显示edText值。 在按钮(bt)下面有另一个按钮(bt1),它再次显示edText值。 此按钮还必须显示r行和4列EditText视图,即用户必须在EditText中输入r * 4输入数据。 我无法清除错误,我不知道如何清除该错误。 请帮助我。非常感谢任何帮助!!

编辑:我现在的代码

public class Ybus_Activity extends Activity 
{
  int i;
  TableLayout myTL;
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_ybus);
  final LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
        LayoutParams.WRAP_CONTENT);
  final LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
  TextView getData=new TextView(this);
  getData.setText("Enter the number of LineData : ");
  getData.setId(5);
  getData.setLayoutParams(params);
  main.addView(getData);
  final EditText edText = new EditText(this);
  edText.setId(3);
  edText.setLayoutParams(params);
  edText.setWidth(100);
  edText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
  edText.setInputType(InputType.TYPE_CLASS_NUMBER);
  edText.setKeyListener(DigitsKeyListener.getInstance());
  edText.setMaxLines(1);
  main.addView(edText );
  Button bt = new Button(this);
  bt.setText("Click to enter Linedata");
  bt.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
  main.addView(bt);
  final TextView text = new TextView(this);
  bt.setOnClickListener(new View.OnClickListener() 
  { 
    public void onClick(View v) 
    {
        String ed=edText.getText().toString(); 

        try{
          i =Integer.parseInt(ed);
         //setting value here
          text.setText(i+"");
         //or you can do like this
         //text.setText(String.valueOf(i));
        }catch(NumberFormatException ex){
          text.setText("Value at TextView is not a valid integer");
        }
    }
});
main.addView(text);
final TextView text2 = new TextView(this);
Button two = new Button(this);
two.setText("Second");
main.addView(two);
main.addView(text2);
two.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v) {
        text2.setText(String.valueOf(i));
        main.addView(createTL(i));
    }
  });
}
 public TableLayout createTL(int r)
 {
    int c=4;
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
myTL=new TableLayout(Ybus_Activity.this);
myTL.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.WRAP_CONTENT));
    myTL.setStretchAllColumns(true);
TableRow[] tr = new TableRow[r];
    EditText[][] et = new EditText[r][c];
    for(int i=0;i<r;i++)
    {
       tr[i] = new TableRow(this);
      for(int j=0;j<4;j++)
          {
            et[i][j] = new EditText(this);
            et[i][j].setText("o.oo");
            et[i][j].setLayoutParams(params);
            et[i][j].setWidth(100);
            et[i][j].setImeOptions(EditorInfo.IME_ACTION_NEXT);
            et[i][j].setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
            et[i][j].setKeyListener(DigitsKeyListener.getInstance());
            et[i][j].setMaxLines(1);
            tr[i].addView(et[i][j]);
            }
      myTL.addView(tr[i]);
    }
//I already have LinearLayout named "main" under onCreate() method...
//main.addView(myTL);
return myTL;
}
}

编辑:我现在的logcat 我的LogCat

02-23 10:33:19.413: D/AndroidRuntime(1819): Shutting down VM
02-23 10:33:19.413: W/dalvikvm(1819): threadid=1: thread exiting with uncaught exception (group=0xb2fa2288)
02-23 10:33:19.502: E/AndroidRuntime(1819): FATAL EXCEPTION: main
02-23 10:33:19.502: E/AndroidRuntime(1819): java.lang.ArithmeticException: divide by zero
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.widget.TableLayout.mutateColumnsWidth(TableLayout.java:583)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.widget.TableLayout.shrinkAndStretchColumns(TableLayout.java:572)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.widget.TableLayout.measureVertical(TableLayout.java:470)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.widget.TableLayout.onMeasure(TableLayout.java:435)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.View.measure(View.java:15172)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.View.measure(View.java:15172)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.View.measure(View.java:15172)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.View.measure(View.java:15172)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.View.measure(View.java:15172)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.Choreographer.doFrame(Choreographer.java:525)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.os.Handler.handleCallback(Handler.java:615)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.os.Looper.loop(Looper.java:137)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at android.app.ActivityThread.main(ActivityThread.java:4745)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at java.lang.reflect.Method.invokeNative(Native Method)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at java.lang.reflect.Method.invoke(Method.java:511)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-23 10:33:19.502: E/AndroidRuntime(1819):     at dalvik.system.NativeStart.main(Native Method)
02-23 10:33:19.632: D/dalvikvm(1819): GC_CONCURRENT freed 151K, 3% free 11037K/11271K, paused 56ms+67ms, total 188ms

1 个答案:

答案 0 :(得分:1)

你不能在这里使用填充。 Fill不是创建新行,它只是将同一对象的副本添加到数组的每个索引中。这意味着你第一次调用myTL.addView(tr [i]);您为数组中的所有索引设置父级。第二次失败,因为你已经有了父母。您需要在循环中创建单独的表行,而不是调用fill。