Slidingtablayout内的SwipableRefreshLayout崩溃应用程序

时间:2015-01-31 22:05:51

标签: android android-layout android-fragments

使用SwipableTabsLayout和SwipeRefreshLayout时遇到一些问题。 每当我启动应用程序时,测试拉动刷新,应用程序崩溃:

01-31 23:00:49.376    2181-2181/com.larswise.ru2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.larswise.ru2, PID: 2181
    java.lang.NullPointerException
            at android.support.v4.widget.SwipeRefreshLayout.startRefresh(SwipeRefreshLayout.java:530)
            at android.support.v4.widget.SwipeRefreshLayout.onTouchEvent(SwipeRefreshLayout.java:484)
            at android.view.View.dispatchTouchEvent(View.java:8112)

我有一个包含3个标签的SwipableTabLayout;对于每个标签我都有一个片段 视图分页器适配器为所选选项卡的视图膨胀,如下所示:

@Override
public Object instantiateItem(ViewGroup container, int position) {
    int posFragment;
    switch(position) {
        case 0: posFragment = R.layout.root_feed_layout; break;
        case 1: posFragment = R.layout.discover_view; break;
        case 2: posFragment = R.layout.settings_view; break;
        default: posFragment = R.layout.feed_layout;
    }

    View view = getActivity().getLayoutInflater().inflate(posFragment,
            container, false);
    container.addView(view);
    return view;
}

在其中一个布局中," root_feed_layout",我有一个片段引用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.larswise.ru2.fragments.FeedFragment"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:id="@+id/feed_root_layout">

    <fragment
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:name="com.larswise.ru2.fragments.FeedFragment"/>

</RelativeLayout>

这是片段的布局,&#34; FeedFragment&#34;我有SwipeRefreshLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/feed_root"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_height="fill_parent">
    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_paren"
        android:id="@+id/swipe_refresh_layout">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff">

            <ListView
                android:id="@+id/post_list"
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="2"
                android:cacheColorHint="#000000"
                android:divider="#D5DDDE"
                android:dividerHeight="1dp"
                android:drawSelectorOnTop="false"
                android:focusableInTouchMode="false"
                android:footerDividersEnabled="false"
                android:headerDividersEnabled="true"
                android:listSelector="@android:color/transparent" />

            </ScrollView>
        </android.support.v4.widget.SwipeRefreshLayout>
<LinearLayout
    android:id="@+id/footer"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:gravity="center|bottom">

        <Button
            android:id="@+id/newButton"
            style="@style/BottomBarButton"
            android:text="@string/menu_new" />
        <View
            android:layout_width="1dip"
            android:layout_height="35dp"
            />
        <Button
            android:id="@+id/extraButton"
            style="@style/BottomBarButton"
            android:text="@string/menu_test" />
</LinearLayout>
</LinearLayout>

有关如何解决此问题或为何失败的任何提示都表示赞赏。 谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,我刚刚错过了mSwipeRefresh.setOnRefreshListener(this); 这是我的片段,实现了SwipeRefreshLayout.OnRefreshListener。