Проведите пальцем вниз, чтобы обновить сообщение об ошибке recycleview

Я создаю действие обновления, когда пользователь проводит вниз в 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;

найти идентификатор

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

И установить OnRefreshListener

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


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

Но при запуске приложения в строке отображается ошибка: 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 ответ

Сначала инициализируйте SwipeRefreshLayout

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

затем установите Listener:

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

    LoadDtaWeb();

}
});
Другие вопросы по тегам