android - 指定的子项已有父项。您必须先在子项的父级上调用 removeView()——动态添加按钮时出现此错误

标签 android android-studio

我有一个应用程序,我在其中为列表中的 friend 数量动态添加按钮。我可以添加 1 个 friend 并查看 friend 列表。但是,当我添加第二个 friend 并尝试查看 FriendList 时,出现此错误--

The specified child already has a parent. You must call removeView() on the child's parent first

这是我的代码--

public class FriendsList extends AppCompatActivity implements View.OnClickListener {    

    @SuppressWarnings("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friends_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        createButtons();    
    }

    private void createButtons() {
        int size = FriendSearch.allFriends.size();
        for (int i = 0; i <= size - 1; i++) {
            LinearLayout layout = (LinearLayout) findViewById(R.id.content_friends_list);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);

            layout.setOrientation(LinearLayout.VERTICAL);

            Button btn = new Button(this);

            for (int j = 0; j <= size - 1; j++) {
                String name = FriendSearch.allFriends.get(j).getUserName();
                btn.setText(name);

                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //goes to this friend's profile page
                        FriendSearch.setIsFriend(true);
                        startActivity(new Intent(FriendsList.this, Profile.class));
                    }
                });
                layout.addView(btn);
            }    
        }
    }   

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_friends_list, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch(item.getItemId()){
            case R.id.addFriendOption:
                startActivity(new Intent(this, FriendSearch.class));
                break;

            case R.id.myProfileOption:
                FriendSearch.setIsFriend(false);
                startActivity(new Intent(this, Profile.class));
                break;

            case android.R.id.home:
                NavUtils.navigateUpTo(this, getIntent());
                return true;
        }

        return true;
    }

    @Override
    public void onClick(View v) {    
    }
}

我认为它希望我在再次调用页面时清除 View ,以便可以添加所有按钮。但在我尝试过的每一种方式中,我都会遇到错误或空白 Activity 。

最佳答案

要么移动:

Button btn = new Button(this);

进入您的for (int j = 0; j <= size - 1; j++)循环或移动:

layout.addView(btn);

外面你的for (int j = 0; j <= size - 1; j++)循环。

就目前而言,您正在尝试添加相同的 Button几次,每次通过一次 for (int j = 0; j <= size - 1; j++)循环。

关于android - 指定的子项已有父项。您必须先在子项的父级上调用 removeView()——动态添加按钮时出现此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40580066/

相关文章:

android - 可滚动寻呼适配器

android - 当我通过 android studio 运行时不会自动启动应用程序

android - 如何使用 token 将视频发布(插入)到youtube?

java - 自定义布局未在 mainActivity 类中检测到?

android - 在 android M 中以编程方式更改 wifi 配置的开发?删除保存的 wifiaswords 不工作

android - 问题 : While stationary Android GPS gives different location updates - Closed

android - 通过 "assets"文件夹下的文件夹用ExoPlayer播放视频

android - 动态功能模块中 Fragments 的 fragmentScenario Espresso 测试

android - 禁用客户端对搜索栏的更改

android - 来自 google direction API 的 JSON 解析