setOnRefreshListener片段错误

时间:2015-02-24 22:31:40

标签: android android-fragments

我正在关注这个tutorial,我正在尝试在我的片段上实现这个女巫是一个包含ListViewAdapter的ListFragment。 问题是我正在执行APP,它给了我错误。

这是我的ListView适配器XML:

<?xml version="1.0" encoding="utf-8"?>
<!-- the parent view - provides the gray background -->
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center_vertical"
    android:background="@color/frame_background"
    android:padding="5dp" >

    <!-- the innner view - provides the white rectangle -->
    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/frame" >

        <!-- the icon view -->
        <ImageView android:id="@+id/ivIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:contentDescription="@string/icon_content_description"
            android:scaleType="fitXY"
            android:layout_alignParentLeft="true" />

        <!-- the container view for the title and description -->
        <RelativeLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/ivIcon"
            android:layout_centerVertical="true" >

            <!-- the title view -->
            <TextView android:id="@+id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#0000FF"
android:textSize="17sp"
                android:textAppearance="@android:style/TextAppearance.Medium" />

            <!-- the description view -->
            <TextView android:id="@+id/tvDiscount"
                android:layout_below="@id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FF0000"

                android:textAppearance="@android:style/TextAppearance.Small" />
            <TextView android:id="@+id/tvPrice"
                android:layout_below="@id/tvDiscount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FF0000"
                android:textAppearance="@android:style/TextAppearance.Small" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tvDatas"
                android:layout_below="@id/tvPrice"
                android:textColor="#FF0000"
                android:textAppearance="@android:style/TextAppearance.Small"/>
        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

    </android.support.v4.widget.SwipeRefreshLayout>

我放置SwipeRefreshLayout,然后放在我的ListFragment上,我把它放在:

 swipeLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(this);
    swipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_orange_light,
            android.R.color.holo_red_light);

在我的OnCreate上,然后我创建了一个onRefresh()方法,它崩溃了以下LogCat错误:

Process: info.androidhive.slidingmenu, PID: 2914
java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.SwipeRefreshLayout.setOnRefreshListener(android.support.v4.widget.SwipeRefreshLayout$OnRefreshListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.SwipeRefreshLayout.setOnRefreshListener(android.support.v4.widget.SwipeRefreshLayout$OnRefreshListener)' on a null object reference
        at info.androidhive.slidingmenu.MisOfertasFragment.onCreate(MisOfertasFragment.java:43)
        at android.app.Fragment.performCreate(Fragment.java:2031)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
        at android.app.BackStackRecord.run(BackStackRecord.java:833)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
        at android.app.Activity.performStart(Activity.java:5948)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)

线指向的是swipeLayout.setOnRefreshListener(this);

请告诉我我错过了什么或我做错了什么。

1 个答案:

答案 0 :(得分:0)

将此代码粘贴到片段

onActivityCreated 方法中
swipeLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(this);
    swipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_orange_light,
            android.R.color.holo_red_light);