无法从布局中删除视图

Posted

技术标签:

【中文标题】无法从布局中删除视图【英文标题】:Cannot remove a view from layout 【发布时间】:2012-10-30 20:55:13 【问题描述】:

我正在创建一个小应用程序,您可以在其中通过单击按钮添加或删除表格行。每当我单击“添加行”按钮时,该按钮都会在布局中添加一个表格行并执行它应该执行的操作。但是,“删除行”不会删除新添加的表格行,但会像预期的那样减少(int count)。我已经工作和搜索了大约 4 到 5 个小时,但我没有运气。代码如下:

int count = 0;

public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);       
    setContentView(R.layout.activity_main);
    Button buttonAdd = (Button) findViewById(R.id.button1);
    Button buttonDel = (Button) findViewById(R.id.button2);
    buttonAdd.setOnClickListener(this);
    buttonDel.setOnClickListener(this);




public void onClick(View v) 
    TableLayout tableLayout1;
    EditText editText1;
    EditText editText2;
    TableRow tempRow;
    EditText tempText1;
    EditText tempText2;
    TableRow tableRow1;

    tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
    editText1 = (EditText) findViewById(R.id.editText1);
    editText2 = (EditText) findViewById(R.id.editText2);
    tempRow = new TableRow(MainActivity.this);
    tempText1 = new EditText(MainActivity.this);
    tempText2 = new EditText(MainActivity.this);
    tempText1.setInputType(InputType.TYPE_CLASS_TEXT);
    tempText2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
    tableRow1 = (TableRow) findViewById(R.id.tableRow1);

    tempRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    tempText1.setLayoutParams(editText1.getLayoutParams());
    tempText2.setLayoutParams(editText2.getLayoutParams());
    switch(v.getId())
    case R.id.button1:
        if(count != 9)
            count++;

            tempRow.addView(tempText1);
            tempRow.addView(tempText2);
            tableLayout1.addView(tempRow);
         else 
            final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
            alertDialog.setTitle("Error");
            alertDialog.setMessage("You can only have 10 rows!");

            alertDialog.setButton("Ok", new DialogInterface.OnClickListener() 
               public void onClick(DialogInterface dialog, int which) 
                  alertDialog.dismiss();
               
            );

            alertDialog.show();
        
        break;
    case R.id.button2:
        if(count != 0)
            count--;
            tempRow.removeView(tempText1);
            tempRow.removeView(tempText2);
            tableLayout1.removeView(tempRow);
            //tableLayout1.invalidate();

         else 
            final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
            alertDialog.setTitle("Error");
            alertDialog.setMessage("You must have at least one row!");

            alertDialog.setButton("Ok", new DialogInterface.OnClickListener() 
               public void onClick(DialogInterface dialog, int which) 
                  alertDialog.dismiss();
               
            );

            alertDialog.show();
        
        break;
        default:
            //Do something;
    

    

.

10-30 21:22:23.379: E/androidRuntime(3970): FATAL EXCEPTION: main
10-30 21:22:23.379: E/AndroidRuntime(3970): java.lang.NullPointerException
10-30 21:22:23.379: E/AndroidRuntime(3970): at com.android.hopegpacalc.MainActivity.onClick(MainActivity.java:57)
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.view.View.performClick(View.java:3511)
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.view.View$PerformClick.run(View.java:14105)
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.os.Handler.handleCallback(Handler.java:605)
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.os.Handler.dispatchMessage(Handler.java:92)
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.os.Looper.loop(Looper.java:137)
10-30 21:22:23.379: E/AndroidRuntime(3970): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-30 21:22:23.379: E/AndroidRuntime(3970): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 21:22:23.379: E/AndroidRuntime(3970): at java.lang.reflect.Method.invoke(Method.java:511)
10-30 21:22:23.379: E/AndroidRuntime(3970): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-30 21:22:23.379: E/AndroidRuntime(3970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-30 21:22:23.379: E/AndroidRuntime(3970): at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

【参考方案1】:

这是组织问题,试试这个:

public void onClick(View v) 
    // These should be class variables, like count, you don't need to re-find it on every click
    TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
    EditText editText1 = (EditText) findViewById(R.id.editText1);
    EditText editText2 = (EditText) findViewById(R.id.editText2);

    switch(v.getId())
    case R.id.button1:
        if(count != 9)
            count++;

            // Create the row only when the add button is clicked
            TableRow tempRow = new TableRow(MainActivity.this);
            EditText tempText1 = new EditText(MainActivity.this);
            EditText tempText2 = new EditText(MainActivity.this);
            tempText1.setInputType(InputType.TYPE_CLASS_TEXT);
            tempText2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

            tempRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            tempText1.setLayoutParams(editText1.getLayoutParams());
            tempText2.setLayoutParams(editText2.getLayoutParams());

            tempRow.addView(tempText1);
            tempRow.addView(tempText2);
            tableLayout1.addView(tempRow);
         else 
            // Consider using Activity's showDialog() method, to reuse this "error" dialog rather than create a new one every time
            final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
            alertDialog.setTitle("Error");
            alertDialog.setMessage("You can only have 10 rows!");

            alertDialog.setButton("Ok", new DialogInterface.OnClickListener() 
                public void onClick(DialogInterface dialog, int which) 
                    alertDialog.dismiss();
                
            );

            alertDialog.show();
        
        break;
    case R.id.button2:
        if(count != 0)
            // Remove the last row at count or use "tableLayout1.getChildCount() - 1"
            tableLayout1.removeView(tableLayout1.getChildAt(count));
            count--;
         else 
            // Consider reusing the other "error" Dialog and simply changing the message
            final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
            alertDialog.setTitle("Error");
            alertDialog.setMessage("You must have at least one row!");

            alertDialog.setButton("Ok", new DialogInterface.OnClickListener() 
                public void onClick(DialogInterface dialog, int which) 
                    alertDialog.dismiss();
                
            );

            alertDialog.show();
        
        break;
    default:
        //Do something;
    

【讨论】:

您先生是个天才。谢谢!我现在唯一的问题是,在添加一行后,删除该行,然后尝试再次添加一个,我在第 57 行收到 NullPointerException。它将异常指向“tempText1.setLayoutParams(editText1.getLayoutParams());” 这很奇怪,可以将所有 LogCat 错误编辑到您的问题中吗?所以我可以看到发生了什么。【参考方案2】:

每次执行 onClick 时,您的代码都会创建一个新的 TableRow。只有在要添加行时才应该这样做。当您想删除一个时,您必须使用findViewById( id ) 在现有视图中找到要删除的行,然后在该对象上运行removeView

case R.id.button2:
    if ( count != 0 ) 
        count--;
        tempRow = (TableRow)findViewById( idoftablerowtodelete );
        tableLayout1.removeView( tempRow );

这确实意味着您必须在创建表格时使用setId 在每个表格行上设置一个 ID,并对其进行跟踪。

【讨论】:

【参考方案3】:

您的删除代码是“删除”实际上不在表中的临时视图(例如tempText1) - 它们是在onClick 方法中动态创建的。不要在进入onClick 后立即实例化所有这些对象,而是在每个 switch 语句案例的主体中这样做。对于添加按钮,只需复制和粘贴即可。对于删除,将对象设置为等于要删除的行。

您似乎没有办法选择要删除的行,但假设您在其他地方执行此操作,您可以使用TableLayout.removeViewAt(int index) 来选择哪一行。如果你有视图本身而不是索引,还有TableLayout.removeView(View view)

【讨论】:

以上是关于无法从布局中删除视图的主要内容,如果未能解决你的问题,请参考以下文章

使用带有选项卡布局的自定义视图时无法从选项卡中删除填充

以编程方式从超级视图中删除视图后更新约束/框架(自动布局)

iOS 从超级视图中删除对象会破坏自动布局并禁用滚动视图

从布局中删除视图并让其他元素重新组织自己

使用自动布局时无法隐藏或删除 UICollectionViewCell 中的视图

从 XML 布局中的自定义视图中删除包前缀