Android 活动:必须对子对象调用removeView();她先是父母
Android Studio 3.1,Java 1.8。 我想通过编程将视图添加到GridLayout。 所以我使用方法addView() 这里是我的活动代码:Android 活动:必须对子对象调用removeView();她先是父母,android,Android,Android Studio 3.1,Java 1.8。 我想通过编程将视图添加到GridLayout。 所以我使用方法addView() 这里是我的活动代码: public class ProfileActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedIn
public class ProfileActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = getWindow().getDecorView().getRootView();
View profileCategoryActive = inflater.inflate(R.layout.profile_category_active, (ViewGroup) view, false);
TextView categoryNameTextView = profileCategoryActive.findViewById(R.id.categoryNameTextView);
for (int index = 0; index < categoryCount; index++) {
categoryNameTextView.setText("Hello " + index);
gridLayout.addView(profileCategoryActive);
}
}
}
您多次添加同一视图
for (...) {
// can't add _same_ view multiple times!
gridLayout.addView(profileCategoryActive);
}
一个视图只能将一个父视图/子视图添加到另一个视图中,因此存在例外
如果要将多个视图添加到布局中,则需要将多个视图充气,然后分别添加
for (...) {
View view = inflate(..) // inflate new view
layout.addView(view); // add new view
}
您多次添加同一视图
for (...) {
// can't add _same_ view multiple times!
gridLayout.addView(profileCategoryActive);
}
一个视图只能将一个父视图/子视图添加到另一个视图中,因此存在例外
如果要将多个视图添加到布局中,则需要将多个视图充气,然后分别添加
for (...) {
View view = inflate(..) // inflate new view
layout.addView(view); // add new view
}
该错误消息是自补偿的
if(profileCategoryActive.getParent()!=null)
((ViewGroup)profileCategoryActive.getParent()).removeView(profileCategoryActive); // Remove view
gridLayout.addView(profileCategoryActive);
该错误消息是自补偿的
if(profileCategoryActive.getParent()!=null)
((ViewGroup)profileCategoryActive.getParent()).removeView(profileCategoryActive); // Remove view
gridLayout.addView(profileCategoryActive);