名为NewsCenterFragment的片段将替换DrawerActivity中的FramLayout。问题是当我将viewPager滑动到下一张幻灯片时,程序将崩溃。给出的异常如下:java.lang.IllegalStateException:指定的子节点已经有父节点。您必须首先在子节点的父节点上调用removeView()。
public class DrawerActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,Serializable {
private FragmentManager fm;
private NavigationView navigationView;
private NewsMenu data;
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
radioGroup = (RadioGroup) findViewById(R.id.content_drawer_radioGroup);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
checkCacheForNewsCenterSlideBar();
changeNewsSlideBar();
Fragment homeFragment = new HomeFragment();
fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_drawer_content,homeFragment).commit();
getSupportActionBar().setTitle("Home page");
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.content_drawer_radioGroup_shouye:
Fragment homeFragment = new HomeFragment();
fm.beginTransaction().replace(R.id.content_drawer_content,homeFragment).commit();
getSupportActionBar().setTitle("Home page");
break;
case R.id.content_drawer_radioGroup_newscenter:
Fragment newsCenterFragment = new NewsCenterFragment(findNewsTabTitle(),DrawerActivity.this);
fm.beginTransaction().replace(R.id.content_drawer_content,newsCenterFragment).commit();
getSupportActionBar().setTitle("News center");
break;
case R.id.content_drawer_radioGroup_smartservice:
break;
case R.id.content_drawer_radioGroup_goveraffairs:
break;
case R.id.content_drawer_radioGroup_setting:
break;
}
}
});
}
public class NewsCenterFragment extends Fragment {
private ViewPager viewPager;
private Activity mActivity;
private ArrayList<String> title;
private ArrayList<View> views;
public NewsCenterFragment(ArrayList<String> title, Activity activity){
this.title = title;
this.mActivity = activity;
}
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(mActivity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fragment_newscenter_layout,container,false);
viewPager = (ViewPager) view.findViewById(R.id.fragment_newscenter_viewPager);
views = new ArrayList<View>();
View tempLayout = inflater.inflate(R.layout.news_title_layout,container,false);
for(int i=0;i<title.size();i++){
views.add(tempLayout);
}
viewPager.setAdapter(new NewsMenuDetailAdapter());
return view;
}
class NewsMenuDetailAdapter extends PagerAdapter{
@Override
public int getCount() {
return views.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==(LinearLayout)object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
String tabTitle = title.get(position);
View view = views.get(position);
TextView textView = (TextView) view.findViewById(R.id.news_title_layout_title);
textView.setText(tabTitle);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
}
java.lang.IllegalStateException:指定的子级已有父级。您必须先在孩子的父母身上调用removeView()。
我一直在寻找解决方案几个小时。请帮帮我。谢谢。
答案 0 :(得分:0)
看起来你的views array
中的一个对象有N个引用,从这里开始:
View tempLayout = inflater.inflate(R.layout.news_title_layout,container,false);
for(int i=0;i<title.size();i++){
views.add(tempLayout);
}
因此,当您的适配器开始返回项目时,它首次成功附加了此视图(tempLayout
),但在第二次失败时,因为tempLayout (i.e. views[i])
已经拥有上一次调用的父项{{1 }}
您应该提供另一个虚增的视图,或在致电instantiateItem
((ViewGroup)child.getParent()).removeView(child)