解决问题:fragment does not have a view
要点:要在onViewCreated()中使用viewpager,不能在onCreateView()中使用。
public class TestDialog extends DialogFragment {
View view;
ViewPager vp ;
MyAdapter myAdapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_FRAME,R.style.Mdialog);
}
@Override
public void onStart() {
super.onStart();
Window win = getDialog().getWindow();
win.setBackgroundDrawable( new ColorDrawable(Color.TRANSPARENT));
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics( dm );
WindowManager.LayoutParams params = win.getAttributes();
params.gravity = Gravity.BOTTOM;
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
win.setAttributes(params);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.test2, container);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
myAdapter = new MyAdapter(getChildFragmentManager());
vp = (ViewPager) view.findViewById(R.id.vp);
vp.setAdapter(myAdapter);
}
}
网友评论