向下滑动以刷新recycleview显示错误

时间:2017-11-05 03:31:13

标签: android nullpointerexception swiperefreshlayout android-swipe

我在用户在recycleview中向下滑动时创建了刷新操作,但它没有用,任何人都可以帮助我。

布局:

<?xml version="1.0" encoding="utf-8"?>

 <android.support.v4.widget.SwipeRefreshLayout 

   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:id="@+id/swipeRefreshLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@color/viewBg"
   app:layout_behavior="@string/appbar_scrolling_view_behavior"
   tools:context="com.example.hieul.hismart.BookActivity"
   tools:showIn="@layout/activity_book">

<android.support.v7.widget.RecyclerView
     android:id="@+id/recycler_view"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:clipToPadding="false"
     android:scaleType="fitXY"
     android:background="@color/colorButton"
     android:scrollbars="vertical" />

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

然后声明变量

SwipeRefreshLayout mSwipeRefreshLayout;

找到ID

  mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);

和setOnRefreshListener

 mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {


            //Define your task here .
            LoadDtaWeb();
            if (tt = true) {
                mSwipeRefreshLayout.setRefreshing(false);
                tt = false;
            }
        }
    });

但是当运行app时,它会在行显示错误:mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){

这是日志文件

11-05 10:26:08.473 9414-9414/com.example.hieul.hismart E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.hieul.hismart, PID: 9414
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hieul.hismart/com.example.hieul.hismart.BookActivity}: 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:3253)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                                                                         at android.app.ActivityThread.access$1100(ActivityThread.java:221)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:158)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:7225)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                      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 com.example.hieul.hismart.BookActivity.onCreate(BookActivity.java:124)
                                                                         at android.app.Activity.performCreate(Activity.java:6876)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
                                                                         at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:158) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:7225) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
11-05 10:26:12.123 9414-9414/com.example.hieul.hismart I/Process: Sending signal. PID: 9414 SIG: 9

1 个答案:

答案 0 :(得分:-1)

首先初始化SwipeRefreshLayout

 mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);

然后设置Listener:

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
{
@Override
public void onRefresh() {

    LoadDtaWeb();

}
});