No View Found For Id ... For Fragment In A Dialog
I want show 2 tabs in a custom Dialog in an Activity, but I am getting the following error. Error: No view found for id 0x7f0f0134 (com.hiro.chatio:id/viewPage_theme) for fragment
Solution 1:
You're getting that Exception because the Activity
's FragmentManager
cannot find View
s in a Dialog
, as its layout is not attached to the Activity
's hierarchy. In order to use Fragment
s in a Dialog
, you'll have to use a DialogFragment
, passing its child FragmentManager
to the PagerAdapter
to handle the transactions.
As with any regular Fragment
, we can inflate the layout in onCreateView()
, and set it up in onViewCreated()
. We'll also override the onCreateDialog()
method to modify the window settings there.
publicclassThemeDialogFragmentextendsDialogFragment {
publicThemeDialogFragment() {}
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.blog_theme_picker, container, false);
}
@OverridepublicvoidonViewCreated(View view, Bundle savedInstanceState) {
ButtonpickColor= (Button) view.findViewById(R.id.pick_color_btn);
Buttondefault_color= (Button) view.findViewById(R.id.default_color);
TabLayoutmTabLayout= (TabLayout) view.findViewById(R.id.main_tabs_theme);
CustomViewPagermViewPager= (CustomViewPager) view.findViewById(R.id.viewPage_theme);
ThemePagerAdaptermThemePagerAdapter=newThemePagerAdapter(getChildFragmentManager());
mViewPager.setAdapter(mThemePagerAdapter);
mViewPager.setCurrentItem(0);
mViewPager.setPagingEnabled(false);
mTabLayout.setupWithViewPager(mViewPager);
}
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
Dialogd=super.onCreateDialog(savedInstanceState);
d.getWindow().getAttributes().windowAnimations = R.style.SlideUpDialogAnimation;
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setCanceledOnTouchOutside(false);
return d;
}
}
You can see that everything you had in the onClick()
method is now handled in the DialogFragment
, and that method becomes simply:
pick_color.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
newThemeDialogFragment().show(getSupportFragmentManager(), "theme");
}
}
);
Post a Comment for "No View Found For Id ... For Fragment In A Dialog"