java - 使用循环删除某些布局 View

标签 java android loops layout

我正在使用添加到布局中的方法动态创建 View 。 我正在尝试实现另一个按钮,该按钮应该访问布局并使用循环删除它们,但我的应用程序似乎一直崩溃。 我试图用循环来完成它,因为我不想通过方法 removeAllViews()removeAllViewsInLayout() 删除所有 View 。

public void RemoveViews() {

    ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constraintId);

    // I'd like to keep a text view and a button, but remove the rest.

    TextView textView1 = (TextView) findViewById(R.id.textView1);
    int textView1Id = textView1.getId();
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    int fabId = fab.getId();

    int count = layout.getChildCount();

    for(int i=0;i<count;i++) {
        View childPos = layout.getChildAt(i);
        int childId = layout.getChildAt(i).getId();

        // Here I try to detect whether the child is the text view or button.

        if(textView1Id == childId || fabId == childId) {}
        else {
            // The line below causes the crashing.
            layout.removeView(childPos);

            // This one also crashes:
            //layout.removeViewAt(i);
        }
    }
}

这是 activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintId"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:id="@+id/textView1"
    android:onClick="onClick_textView1"
    android:padding="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:gravity="center"
    android:text="@string/text1"
    android:textColor="#808080"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:srcCompat="@drawable/ic_refresh_black_24dp" />

</android.support.constraint.ConstraintLayout>

我认为这是错误的 logcat(致命异常:主要):

2018-12-31 14:57:33.004 7507-7507/com.example.test4 E/AndroidRuntime: FATAL 
EXCEPTION: main
Process: com.example.test4, PID: 7507
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
    at android.view.View.performClick(View.java:6294)
    at android.view.View$PerformClick.run(View.java:24774)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:172)
    at android.app.ActivityThread.main(ActivityThread.java:6590)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:6294) 
    at android.view.View$PerformClick.run(View.java:24774) 
    at android.os.Handler.handleCallback(Handler.java:790) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:172) 
    at android.app.ActivityThread.main(ActivityThread.java:6590) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference
    at com.example.test4.MainActivity.RemoveKeyboardList(MainActivity.java:115)
    at com.example.test4.MainActivity.onClick_textView1(MainActivity.java:64)
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
    at android.view.View.performClick(View.java:6294) 
    at android.view.View$PerformClick.run(View.java:24774) 
    at android.os.Handler.handleCallback(Handler.java:790) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:172) 
    at android.app.ActivityThread.main(ActivityThread.java:6590) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

基本上,我想知道如何在不让应用程序崩溃的情况下执行此操作,以及是否有更好的方法从布局中删除多个 View 。

//

解决方案

感谢 Anmol 的建议,在单独的布局中添加动态创建的 View ,我实现了我正在寻找的东西,尽管我无法使其与循环一起工作。我在这里包含了我的解决方案,因为它与他的有点不同,尽管我接受了他的回答。对不起,它是一样的,但我已经包含了关于如何实现它的所有步骤。请在到期时注明。

第 1 步: 添加另一个布局以包含在activity_main.xml 中。我将其命名为 dynamic_content.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dynamic_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

</android.support.constraint.ConstraintLayout>

第 2 步:activity_main.xml 中包含新布局。

// Add the line below anywhere as far as I know.
<include layout="@layout/dynamic_content" />

第 3 步:仅在新布局中包含所有动态创建的 View 。

ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.dynamic_content);

// Create new text views, buttons, etc.
layout.addView(newTextView);

第 4 步:创建删除它们的函数。

public void RemoveViews() {
    ConstraintLayout dynamic = (ConstraintLayout) findViewById(R.id.dynamic_content);
    dynamic.removeAllViews();
}

第 5 步:将函数添加到按钮。

public void onClick_buttonName(View v)
{
    RemoveViews();
}

完成 - 尽情享受吧!

最佳答案

根据您的用例,您可以在布局中添加一个 containerLayout 并将所有动态 View 添加到该容器,当您想要删除动态 View 时,只需执行以下操作。

public void AddViewToContainer(View view){

    ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.containerfordynamic_view);

    layout.addView(view);

}

public void RemoveViews() {

    ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.containerfordynamic_view);

    layout.removeAllViews();

}

第二个选项是在您的布局上执行 removeAllView() 并再次添加您的 editText 和 Fab 按钮,

但这不应该是首选,因为目前您只有两个 View 要保留,但如果列表增长对于简单操作来说开销太大,所以最好选择第一个也易于管理和操作的解决方案。

谢谢。

关于java - 使用循环删除某些布局 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53984903/

相关文章:

java - Spring data jpa - 更新父级时,如何从数据库中删除与其关联的子级(基于id)并添加新的子级

java - 给定范围内复杂度较低的素数

java - jsp中无法连接mysql数据库

android - startActivity 上的 java.lang.IllegalArgumentException( Intent 、 bundle 动画)

Android,Proguard 'Unknown option ' 和'参数号 9'

java - 保存列表的大小而不更新它

android - 如何在 Mac OS 上为 Android 构建 FFmpeg 可执行二进制文件?

java - 有没有办法从文件中读取值并初始化数组?

Java Selenium 多表结构,遍历所有

sql - 我如何拥有在同一行上回显回声的powershell循环?