安卓 Espresso : ViewPager does not have adapter instance

标签 android android-fragments android-testing android-espresso

我在我的 Android (4.0+) 应用 fragment (在 Activity 中)中使用标签栏。

我想创建 Espresso 测试,但如果我创建主 Activity 并打开 fragment 。我得到这个异常:

java.lang.IllegalStateException: ViewPager does not have adapter instance.
at com.astuetz.PagerSlidingTabStrip.setViewPager(PagerSlidingTabStrip.java:177)
at cz.villamemories.detoxme.staticcontent.StaticContentFragment.onCreateView(StaticContentFragment.java:197)

我在 fragment 中的代码:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

        mViewPagerAdapter = new StaticContentPagerAdapter(
                this.getChildFragmentManager(), mItemsList, categories);

        mPager.setAdapter(mViewPagerAdapter);

        mTabs.setViewPager(mPager); //line 197

你有什么地方可能有问题的提示吗?怎么了?

最佳答案

我在 fragment 中也面临着 ViewPager 的问题。这个问题发生在我身上是因为我的 viewpager 有多个根元素。

@RunWith(JUnit4.class)
public class FragmentTest {

private static final String TAG = "FragmentTest";

@Rule
public ActivityTestRule<MainActivity> mActivity = new ActivityTestRule<MainActivity>(MainActivity.class);

@Test
public void checkTabSwiping() {
    onView(nthChildOf(withId(R.id.main_content), 5)).check(matches(withId(R.id.homecontentgrouppager)));
}

public static Matcher<View> firstChildOf(final Matcher<View> parentMatcher) {
    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("with first child view of type parentMatcher");
        }

        @Override
        public boolean matchesSafely(View view) {

            if (!(view.getParent() instanceof ViewGroup)) {
                return parentMatcher.matches(view.getParent());
            }
            ViewGroup group = (ViewGroup) view.getParent();
            return parentMatcher.matches(view.getParent()) && group.getChildAt(0).equals(view);

        }
    };
}
public static Matcher<View> nthChildOf(final Matcher<View> parentMatcher,
                                       final int childPosition){
    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("with " + childPosition + " child view of type parentMatcher");
        }

        @Override
        public boolean matchesSafely(View view) {
            if (!(view.getParent() instanceof ViewGroup)) {
                return parentMatcher.matches(view.getParent());
            }

            ViewGroup group = (ViewGroup) view.getParent();
            return parentMatcher.matches(view.getParent()) && view.equals(group.getChildAt(childPosition));
        }
    };
}
}

这是我的 homefragment.xml 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="@color/black"
android:orientation="vertical">

<android.support.v4.view.ViewPager
    android:id="@+id/promotionalviewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></android.support.v4.view.ViewPager>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="@dimen/size_5"
    android:gravity="center_horizontal">

    <LinearLayout
        android:id="@+id/viewPagerCountDots"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:orientation="horizontal" />

</RelativeLayout>

希望对您在 Expresso Framework 中创建测试用例有所帮助。

关于安卓 Espresso : ViewPager does not have adapter instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31023977/

相关文章:

android - 带有复选框的 ListView 有问题

android - 在选项卡中具有同步 ListView 的 viewpager

android - 单元测试网络响应。调试时有效,实际运行时无效

android - 重新运行失败的 android 测试

android - JaCoCo 显示 0% 的覆盖率,甚至所有测试都通过了

android - 从 xml 加载 android 布局

java - Android DEX 65k 异常 - Gradle 模块 - 还有其他想法吗?

android - 回到主 Fragment 实例后,FragmentStatePagerAdapter 没有重新创建 Fragments

java - Android 应用程序在重新打开和替换 fragment 时崩溃

android - 将 Future<void> 转换为 RxJava Observable 或 Flowable