-1

ユーザーがリサイクルビューでスワイプダウンしたときにリフレッシュアクションを作成しますが、誰かがうまく動作しません。スワイプしてリサイクルビューを表示するエラーを表示する

レイアウト:

<?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; 

のfind 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; 
      } 
     } 
    }); 
を宣言

しかし、アプリケーションを実行するときには、行でエラーが表示さ:mSwipeRefreshLayout.setOnRefreshListener(新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 
+4

'BookActivity'に間違ったXMLがあります –

答えて

-1

まずその後SwipeRefreshLayout

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

を初期化しますリスナー:

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

    LoadDtaWeb(); 

} 
}); 
関連する問題