2017-12-06 17 views
0

助けてください。私は断片でRecycleViewを使用しています。しかし、私は活動を開始...クラッシュを取得する!私は視野に起因すると思う。なぜ知りません! 誰か知っていますか? こんにちは。私は断片でRecycleViewを使用しています。しかし、私は活動を開始...クラッシュを取得する!私は視野に起因すると思う。なぜ知りません! 誰か知っていますか? logcatでリサイクル開始時のフラグメントクラッシュ時

fragmentHome.java

public class HomeFragment2 extends Fragment { 

List<DataAdapter> ListOfdataAdapter; 
RecyclerView recyclerView; 
String HTTP_JSON_URL = "http://androidblog.esy.es/ImageJsonData.php"; 
String Image_Name_JSON = "image_title"; 
String Image_URL_JSON = "image_url"; 
JsonArrayRequest RequestOfJSonArray ; 
RequestQueue requestQueue ; 
View view ; 
int RecyclerViewItemPosition ; 
RecyclerView.LayoutManager layoutManagerOfrecyclerView; 
RecyclerView.Adapter recyclerViewadapter; 
ArrayList<String> ImageTitleNameArrayListForClick; 


public static HomeFragment2 newInstance() { 


    return new HomeFragment2(); 
} 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 

    View v = inflater.inflate(R.layout.fragment_home2, container, false); 

    ImageTitleNameArrayListForClick = new ArrayList<>(); 

    ListOfdataAdapter = new ArrayList<>(); 

    recyclerView =getActivity(). findViewById(R.id.recyclerview1); 

    recyclerView.setHasFixedSize(true); 

    layoutManagerOfrecyclerView = new LinearLayoutManager(getActivity()); 

    recyclerView.setLayoutManager(layoutManagerOfrecyclerView); 

    JSON_HTTP_CALL(); 

    // Implementing Click Listener on RecyclerView. 
    recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { 

     GestureDetector gestureDetector = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener() { 

      @Override public boolean onSingleTapUp(MotionEvent motionEvent) { 

       return true; 
      } 

     }); 
     @Override 
     public boolean onInterceptTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) { 

      view = Recyclerview.findChildViewUnder(motionEvent.getX(), motionEvent.getY()); 

      if(view != null && gestureDetector.onTouchEvent(motionEvent)) { 

       //Getting RecyclerView Clicked Item value. 
       RecyclerViewItemPosition = Recyclerview.getChildAdapterPosition(view); 

       // Showing RecyclerView Clicked Item value using Toast. 
       Toast.makeText(getActivity(), ImageTitleNameArrayListForClick.get(RecyclerViewItemPosition), Toast.LENGTH_LONG).show(); 
      } 

      return false; 
     } 

     @Override 
     public void onTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) { 

     } 

     @Override 
     public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 

     } 
    }); 

    return v; 

} 

public void JSON_HTTP_CALL(){ 

    RequestOfJSonArray = new JsonArrayRequest(HTTP_JSON_URL, 

      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 

        ParseJSonResponse(response); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      }); 

    requestQueue = Volley.newRequestQueue(getActivity()); 

    requestQueue.add(RequestOfJSonArray); 
} 

public void ParseJSonResponse(JSONArray array){ 

    for(int i = 0; i<array.length(); i++) { 

     DataAdapter GetDataAdapter2 = new DataAdapter(); 

     JSONObject json = null; 
     try { 

      json = array.getJSONObject(i); 

      GetDataAdapter2.setImageTitle(json.getString(Image_Name_JSON)); 

      // Adding image title name in array to display on RecyclerView click event. 
      ImageTitleNameArrayListForClick.add(json.getString(Image_Name_JSON)); 

      GetDataAdapter2.setImageUrl(json.getString(Image_URL_JSON)); 

     } catch (JSONException e) { 

      e.printStackTrace(); 
     } 
     ListOfdataAdapter.add(GetDataAdapter2); 
    } 

    recyclerViewadapter = new RecyclerViewAdapter(ListOfdataAdapter, getActivity()); 

    recyclerView.setAdapter(recyclerViewadapter); 
} 
} 

とエラーになります:

                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358) 
                      at android.app.ActivityThread.access$600(ActivityThread.java:156) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340) 
                      at android.os.Handler.dispatchMessage(Handler.java:99) 
                      at android.os.Looper.loop(Looper.java:153) 
                      at android.app.ActivityThread.main(ActivityThread.java:5297) 
                      at java.lang.reflect.Method.invokeNative(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:511) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
                      at dalvik.system.NativeStart.main(Native Method) 
                     Caused by: java.lang.NullPointerException 
                      at ir.dadevarzjooya.MainActivity2.HomeFragment2.onCreateView(HomeFragment2.java:60) 
                      at android.app.Fragment.performCreateView(Fragment.java:1695) 
                      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885) 
                      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057) 
                      at android.app.BackStackRecord.run(BackStackRecord.java:694) 
                      at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435) 
                      at android.app.Activity.performStart(Activity.java:5131) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2279) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)  
                      at android.app.ActivityThread.access$600(ActivityThread.java:156)  
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)  
                      at android.os.Handler.dispatchMessage(Handler.java:99)  
                      at android.os.Looper.loop(Looper.java:153)  
                      at android.app.ActivityThread.main(ActivityThread.java:5297)  
                      at java.lang.reflect.Method.invokeNative(Native Method)  
                      at java.lang.reflect.Method.invoke(Method.java:511)  
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)  
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)  
                      at dalvik.system.NativeStart.main(Native Method)  

すべてのボディは、アイデアを持っていますか? この問題はビューから発生していると思います...

答えて

3

この行に問題があります。

recyclerView = getActivity().findViewById(R.id.recyclerview1); 

あなたがいない

+1

は本当に働いていた活動から、あなたの方法の初めに初期化されているフラグメントのレイアウトからあなたのリサイクルのビューを取得する必要があります。この

recyclerView = v.findViewById(R.id.recyclerview1); 

とその行を置き換えます。 ) ありがとうございました :))) –