简体   繁体   English

在对话框中添加标签时出现``找不到ID的视图''异常-Android

[英]'No view found for id' exception while adding tabs in dialog - android

I have an activity in which I want to show a dialog with 2 tabs. 我有一个活动,想要显示一个带有2个选项卡的对话框。 I have the following code on button click which will show the dialog: 我在按钮单击上有以下代码,它将显示该对话框:

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();

Adaper for ViewPager is as, Adaper for 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;
        }
    }
}

select_category.xml is, select_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>

When I run this code I'm getting IllegalArgumentException as, 当我运行这段代码时,我得到的是IllegalArgumentException,

java.lang.IllegalArgumentException: No view found for id 0x7f0a00da (com.myapp:id/viewPager) for fragment Fragment java.lang.IllegalArgumentException:找不到片段Fragment的ID 0x7f0a00da(com.myapp:id / viewPager)的视图

The TabLayout I'm using is from Google's design support library ( android.support.design.widget.TabLayout ). 我正在使用的TabLayout来自Google的设计支持库( android.support.design.widget.TabLayout )。

I have no idea where I'm getting wrong. 我不知道我哪里出错了。 Please help! 请帮忙! Any tutorial / guide suggestion would be appreciated. 任何教程/指南的建议,将不胜感激。

I read the documentation on FragmentPagerAdapter @ FragmentPagerAdapter . 我阅读了FragmentPagerAdapter @ FragmentPagerAdapter上的文档。 In the webpage, the getItem() is coded differently than your code. 在网页中, getItem()的编码方式与您的代码不同。 Their suggestion is: 他们的建议是:

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

Notes: 笔记:

  • You also have to define the static method newInstance . 您还必须定义静态方法newInstance
  • I think you have to extend (subclass) a Fragment . 我认为您必须扩展(子类)一个Fragment In the above webpage, it subclass ListFragment . 在上述网页中,它是ListFragment子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM