';找不到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
getItem()
的编码与您的代码不同。他们的建议是:
@Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
注:
- 您还必须定义静态方法
newInstance
- 我认为您必须扩展(子类)一个片段。在上面的网页中,它是子类
ListFragment
FragmentPagerAdapter
的工作示例。仅当我尝试在对话框中创建选项卡时,才会出现上述问题。我已经有了FragmentPagerAdapter
的工作示例。仅当我尝试在对话框中创建选项卡时,才会出现上述问题。