2016-12-22 77 views
-2

此代碼與activity正常工作,但不在fragment ..應用程序崩潰時,此運行在片段 ...當我刪除getData();下面requestQueue應用程序運行,但數據未取..此代碼正常工作,但沒有在活動片段

public class LatestFragment extends Fragment { 

    //Creating a List of superheroes 
    private List<SuperHero> listSuperHeroes; 

    //Creating Views 
    private RecyclerView recyclerView; 
    private RecyclerView.LayoutManager layoutManager; 
    private RecyclerView.Adapter adapter; 

    private RequestQueue requestQueue; 
    private int requestCount = 1; 



    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View v = inflater.inflate(R.layout.latest, container, false); 
     recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView); 
     recyclerView.setHasFixedSize(true); 
     layoutManager = new LinearLayoutManager(getActivity()); 
     recyclerView.setLayoutManager(layoutManager); 

     //Initializing our superheroes list 
     listSuperHeroes = new ArrayList<>(); 
     requestQueue = Volley.newRequestQueue(getActivity()); 

     //Calling method to get data to fetch data 
     getData(); 

     //Adding an scroll change listener to recyclerview 
     // RecyclerView.setOnScrollChangeListener(this); 

      recyclerView.addOnScrollListener(rVOnScrollListener); 

     //initializing our adapter 
     adapter = new CardAdapter(listSuperHeroes, getActivity()); 

     //Adding adapter to recyclerview 
     recyclerView.setAdapter(adapter); 
     return v; 
    } 

    //Request to get json from server we are passing an integer here 
    //This integer will used to specify the page number for the request ?page = requestcount 
    //This method would return a JsonArrayRequest that will be added to the request queue 
    private JsonArrayRequest getDataFromServer(int requestCount) { 
     //Initializing ProgressBar 
     final ProgressBar progressBar = (ProgressBar) getActivity().findViewById(R.id.progressBar1); 

     //Displaying Progressbar 
     progressBar.setVisibility(View.VISIBLE); 
    // setProgressBarIndeterminateVisibility(true); 

     //JsonArrayRequest of volley 
     JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL + String.valueOf(requestCount), 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         //Calling method parseData to parse the json response 
         parseData(response); 
         //Hiding the progressbar 
         progressBar.setVisibility(View.GONE); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         progressBar.setVisibility(View.GONE); 
         //If an error occurs that means end of the list has reached 
         //Toast.makeText(citiesdetail.this, "No More Items Available", Toast.LENGTH_SHORT).show(); 
        } 
       }); 

     //Returning the request 
     return jsonArrayRequest; 
    } 

    //This method will get data from the web api 
    private void getData() { 
     //Adding the method to the queue by calling the method getDataFromServer 
     requestQueue.add(getDataFromServer(requestCount)); 
     //Incrementing the request counter 
     requestCount++; 
    } 

    //This method will parse json data 
    private void parseData(JSONArray array) { 
     for (int i = 0; i < array.length(); i++) { 
      //Creating the superhero object 
      SuperHero superHero = new SuperHero(); 
      JSONObject json = null; 
      try { 
       //Getting json 
       json = array.getJSONObject(i); 

       //Adding data to the superhero object 
       superHero.setImageUrl(json.getString(Config.TAG_IMAGE_URL)); 
       superHero.setName(json.getString(Config.TAG_NAME)); 
       superHero.setPublisher(json.getString(Config.TAG_PUBLISHER)); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      //Adding the superhero object to the list 
      listSuperHeroes.add(superHero); 
     } 

     //Notifying the adapter that data has been added or changed 
     adapter.notifyDataSetChanged(); 
    } 

    //This method would check that the recyclerview scroll has reached the bottom or not 
    private boolean isLastItemDisplaying(RecyclerView recyclerView) { 
     if (recyclerView.getAdapter().getItemCount() != 0) { 
      int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition(); 
      if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1) 
       return true; 
     } 
     return false; 
    } 

    //Overriden method to detect scrolling 
    private RecyclerView.OnScrollListener rVOnScrollListener = new RecyclerView.OnScrollListener(){ 
     @Override 
     public void onScrollStateChanged(RecyclerView recyclerView, 
             int newState) { 
      super.onScrollStateChanged(recyclerView, newState); 
     } 

     @Override 
     public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
      super.onScrolled(recyclerView, dx, dy); 

      if (isLastItemDisplaying(recyclerView)) { 
       getData(); 
      } 
     } 
    }; 
} 
+3

顯示崩潰日誌 –

+0

在那裏我找到崩潰日誌,因爲我沒有看到和崩潰日誌..in事件日誌窗口他們是不是任何錯誤顯示 –

+0

'getActivity()'在'onCreateView()'爲空' –

回答

2

您調用getData從onCreateView(()方法),這導致調用parseData()方法,在這種方法中,你曾用「適配器」對象和你沒有初始化它。在onCreateView()中的getData()方法之後初始化它。所以這可能會導致NullPointerException。

爲此,您可以在parseData()方法中初始化適配器,並僅從此處設置適配器值。

+0

你能告訴我如何在parseData()方法中初始化適配器並設置適配器值 –

+0

//通知適配器數據已被添加或更改 adapter.notifyDataSetChanged(); –

1

崩潰的原因是getActivity().findViewById().setVisibility()getDataFromServer()方法。

你爲什麼從活動視圖中獲得progressBar?也許你的進度是在片段來看,讓你的代碼應該是這樣的:

public class LatestFragment extends Fragment { 
    private ProgressBar progressBar; 

    ... 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.latest, container, false); 

     ... 
     progressBar = (ProgressBar) v.findViewById(R.id.progressBar1); 
     ... 
    } 

    ... 

    private JsonArrayRequest getDataFromServer(int requestCount) { 
     // You don't need the initialization, it was done in onCreateView() in this code. Remove these lines. 
     //final ProgressBar progressBar = (ProgressBar) getActivity().findViewById(R.id.progressBar1); 

     //Displaying Progressbar 
     progressBar.setVisibility(View.VISIBLE); 
     ... 
    } 
    ... 
} 
+0

所以我想做 –

+0

@SyedMuzzammilMian初始化'onCreateView()'中的progressBar,而不是'getDataFromServer()',並在片段視圖中搜索它,而不是在活動中。 –

+0

是@SyedMuzzammilMian不使用getActivit()方法初始化你的片段視圖控件,這也可能導致崩潰。 –

相關問題