2017-08-20 59 views
1

我想顯示自定義Dialog 2個標籤在Activity,但我收到以下錯誤。沒有查看發現ID爲...片段在對話框

錯誤:

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; 
} 
+1

哪裏是'PostColorPickerFragment'代碼? –

+0

而不是僅僅一個'Dialog',你將不得不使用一個'DialogFragment' - 及其子'FragmentManager' - 吃出'ViewPager'工作。該'Activity'的'FragmentManager'將不能夠找到一個'Dialog'任何東西,因爲它沒有連接到'Activity'的層次。 –

+0

Mike M.是對的。你必須使用'DialogFragment'而不是'Dialog'。 – Wizard

回答

1

由於ActivityFragmentManagerDialog中找不到View,因爲它的佈局未附加到Activity的層次結構中,所以您正在獲取該例外。爲了在Dialog使用Fragment S,你必須使用一個DialogFragment,通過其子FragmentManagerPagerAdapter來處理事務。

與任何常規的Fragment一樣,我們可以在onCreateView()中擴充佈局,並將其設置爲onViewCreated()。我們也會覆蓋onCreateDialog()方法來修改那裏的窗口設置。

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處理,並且該方法變得簡單:

pick_color.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      new ThemeDialogFragment().show(getSupportFragmentManager(), "theme"); 
     } 
    } 
); 
+1

tks u,工作正常! –