';找不到id为';在对话框-android中添加选项卡时出现异常

';找不到id为';在对话框-android中添加选项卡时出现异常,android,android-fragments,android-dialog,android-tabs,Android,Android Fragments,Android Dialog,Android Tabs,我有一个活动,我想在其中显示一个包含两个选项卡的对话框。我点击按钮时有以下代码,将显示对话框: Dialog dialog = new Dialog(this); // select_category is having ViewPager and TabLayout inside of a Framelayout dialog.setContentView(R.layout.select_category); ViewPager objViewPager = (ViewPager) dia

我有一个活动,我想在其中显示一个包含两个选项卡的对话框。我点击按钮时有以下代码,将显示对话框:

Dialog dialog = new Dialog(this);

// select_category is having ViewPager and TabLayout inside of a Framelayout
dialog.setContentView(R.layout.select_category);

ViewPager objViewPager = (ViewPager) dialog.findViewById(R.id.viewPager);

objViewPager.setAdapter(new MyTabsAdapter(getSupportFragmentManager()));
TabLayout mTabLayout = (TabLayout) dialog.findViewById(R.id.tabLayout);

mTabLayout.setTabTextColors(getResources().getColorStateList(R.color.tabcolors));    
mTabLayout.setupWithViewPager(objViewPager);
dialog.show();
ViewPager的适配器如下所示:

public class MyTabsAdapter extends FragmentPagerAdapter {

    String[] tabs = {"FIRST", "SECOND"};

    public MyTabsAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0: {
                // for now, returning plain fragment for simplicity
                return new Fragment(); 
            }

            case 1: {
                return new Fragment();
            }

            default:
                return null;
        }
    }
}
选择_category.xml是

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:theme="@style/NoActionBarTheme">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/view_pager_top_margin"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary" />
</FrameLayout>

当我运行这段代码时,我得到了IllegalArgumentException作为

java.lang.IllegalArgumentException:找不到id为0x7f0a00da的视图 (com.myapp:id/viewPager)用于片段

我使用的TabLayout来自谷歌的设计支持库(
android.support.design.widget.TabLayout

我不知道我哪里出错了。请帮忙!
任何教程/指南建议都将不胜感激。

我阅读了FragmentPagerAdapter@上的文档。在网页中,
getItem()
的编码与您的代码不同。他们的建议是:

@Override
public Fragment getItem(int position) {
    return ArrayListFragment.newInstance(position);
}
注:

  • 您还必须定义静态方法
    newInstance
  • 我认为您必须扩展(子类)一个片段。在上面的网页中,它是子类
    ListFragment

我阅读了FragmentPagerAdapter@上的文档。在网页中,
getItem()
的编码与您的代码不同。他们的建议是:

@Override
public Fragment getItem(int position) {
    return ArrayListFragment.newInstance(position);
}
注:

  • 您还必须定义静态方法
    newInstance
  • 我认为您必须扩展(子类)一个片段。在上面的网页中,它是子类
    ListFragment

您应该提及导致错误的代码。我假设它是dialog.findviewbyd(R.id.viewPager)。是吗?另外,发布布局文件“选择类别”。@TheOriginalAndroid:调试时,按钮单击代码运行正常。ViewPager等获取值。控件也会出现在适配器中,然后在抛出异常之后出现。不过,是哪个代码导致了异常?是否返回新片段()?请发布logcat错误,也许这会有很大帮助。@MangeshGhotage您找到了解决方案吗?您应该在代码中提到导致错误的原因。我假设它是dialog.findviewbyd(R.id.viewPager)。是吗?另外,发布布局文件“选择类别”。@TheOriginalAndroid:调试时,按钮单击代码运行正常。ViewPager等获取值。控件也会出现在适配器中,然后在抛出异常之后出现。不过,是哪个代码导致了异常?是否返回新片段()?请发布logcat错误,也许这会有很大帮助。@MangeshGhotage你找到了解决方案吗?我已经有了一个
FragmentPagerAdapter
的工作示例。仅当我尝试在对话框中创建选项卡时,才会出现上述问题。我已经有了
FragmentPagerAdapter
的工作示例。仅当我尝试在对话框中创建选项卡时,才会出现上述问题。