在使用viewpaper与frament时,一开始frament代码如下
public class ManagerUpdateFrament extends Fragment{
private View managerUpdateView;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
managerUpdateView = getActivity().getLayoutInflater().inflate(R.layout.frament_manager_update,(ViewGroup)getActivity().findViewById(R.id.manager_viewpager), false);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return managerUpdateView;
}
}
运行时出现The specified child already has a parent. You must call removeView()的错误
后将代码改成
public class ManagerDownloadFrament extends Fragment {
private View managerDownloadView;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
managerDownloadView = getActivity().getLayoutInflater().inflate(
R.layout.frament_manager_download,
(ViewGroup) getActivity().findViewById(R.id.manager_viewpager),false);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup viewGroup = (ViewGroup) managerDownloadView.getParent();
if(viewGroup!=null){
viewGroup.removeAllViewsInLayout();
}
return managerDownloadView;
}
}