在对话框中找不到片段的ID ...视图 [英] No view found for id ... for fragment in a Dialog
问题描述
我想在Activity
的自定义Dialog
中显示2个选项卡,但是出现以下错误.
I want show 2 tabs in a custom Dialog
in an Activity
, but I am getting the following error.
错误:
No view found for id 0x7f0f0134 (com.hiro.chatio:id/viewPage_theme) for fragment
PostColorPickerFragment{35ffefce #0 id=0x7f0f0134 android:switcher:2131689780:0}
java.lang.IllegalArgumentException: No view found for id 0x7f0f0134 (com.hiro.chatio:id/viewPage_theme)
for fragment PostColorPickerFragment{35ffefce #0 id=0x7f0f0134 android:switcher:2131689780:0}
MainActivity
:
private Button pick_color;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_blog);
pick_color = (Button) findViewById(R.id.create_blog_color_btn);
pick_color.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(CreateBlogActivity.this);
dialog.setContentView(R.layout.blog_theme_picker);
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().getAttributes().windowAnimations = R.style.SlideUpDialogAnimation;
Button pickColor = (Button) dialog.findViewById(R.id.pick_color_btn);
Button default_color = (Button) dialog.findViewById(R.id.default_color);
TabLayout mTabLayout = (TabLayout) dialog.findViewById(R.id.main_tabs_theme);
CustomViewPager mViewPager = (CustomViewPager) dialog.findViewById(R.id.viewPage_theme);
ThemePagerAdapter mThemePagerAdapter = new ThemePagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mThemePagerAdapter);
mViewPager.setCurrentItem(0);
mViewPager.setPagingEnabled(false);
mTabLayout.setupWithViewPager(mViewPager); }); }
ThemePagerAdapter
:
public class ThemePagerAdapter extends FragmentPagerAdapter {
public ThemePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
PostColorPickerFragment postColorPickerFragment = new PostColorPickerFragment();
return postColorPickerFragment;
case 1:
PostThemePickerFragment postThemePickerFragment = new PostThemePickerFragment();
return postThemePickerFragment;
default:
return null;
}
}
@Override
public int getCount() {
return 2;
}
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Color";
case 1:
return "Theme";
default:
return null;
}
}
PostThemePickerFragment
:
public class PostThemePickerFragment extends Fragment {
public PostThemePickerFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.post_theme_picker, container, false);
return view;
}
推荐答案
之所以会出现此异常,是因为Activity
的FragmentManager
在Dialog
中找不到View
,因为它的布局不是附加到Activity
的层次结构.为了在Dialog
中使用Fragment
,您必须使用DialogFragment
,并将其子级FragmentManager
传递给PagerAdapter
来处理事务.
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.
与任何常规Fragment
一样,我们可以在onCreateView()
中填充布局,并在onViewCreated()
中进行设置.我们还将覆盖onCreateDialog()
方法,以在那里修改窗口设置.
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.
public class ThemeDialogFragment extends DialogFragment {
public ThemeDialogFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.blog_theme_picker, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
Button pickColor = (Button) view.findViewById(R.id.pick_color_btn);
Button default_color = (Button) view.findViewById(R.id.default_color);
TabLayout mTabLayout = (TabLayout) view.findViewById(R.id.main_tabs_theme);
CustomViewPager mViewPager = (CustomViewPager) view.findViewById(R.id.viewPage_theme);
ThemePagerAdapter mThemePagerAdapter = new ThemePagerAdapter(getChildFragmentManager());
mViewPager.setAdapter(mThemePagerAdapter);
mViewPager.setCurrentItem(0);
mViewPager.setPagingEnabled(false);
mTabLayout.setupWithViewPager(mViewPager);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog d = super.onCreateDialog(savedInstanceState);
d.getWindow().getAttributes().windowAnimations = R.style.SlideUpDialogAnimation;
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setCanceledOnTouchOutside(false);
return d;
}
}
您可以看到onClick()
方法中的所有内容现在都已在DialogFragment
中处理,并且该方法变得简单:
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(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ThemeDialogFragment().show(getSupportFragmentManager(), "theme");
}
}
);
这篇关于在对话框中找不到片段的ID ...视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!