我有一个 Activity,我想用 2个标签显示一个对话框。 我在按钮单击上有下面的代码,它将显示对话框: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的Adaper为,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;
}
}
}
select_category.xml 是
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:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/view_pager_top_margin"/>
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary"/>
当我运行这个代码时,我得到的是 IllegalArgumentException,java.lang.IllegalArgumentException: 没有找到用于 fragment fragment的id 0 x7f0a00da ( 。com: id/viewpager )的视图
我正在使用的TabLayout来自google支持库的设计( android.support.design.widget.TabLayout ) 。
我不知道我在哪里出错。 请帮助任何教程/指南建议都会被推荐。