2017-01-25 4 views
0

In meiner Android-App möchte ich Daten in Recyclerview Layout anzeigen. hier habe ich einen Code angegeben, bitte verweise darauf. weil ich nicht weiß, was der Grund dafür ist. Bitte hilf mir. Danke im Voraus.Recycler-Ansicht setLayoutManager für eine Nullobjekt-Referenz

fragment.xml

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" 
    android:orientation="vertical"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/rvDisplay" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp"> 

    </android.support.v7.widget.RecyclerView> 
</LinearLayout> 

Hier, ich RAW-Datei von RecyclerView angeben.

raw.xml

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:card_view="http://schemas.android.com/tools" 
    android:id="@+id/cardView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="10dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="10dp" 
    android:background="@color/colorWhite" 
    app:cardElevation="8dp" 
    card_view:cardCornerRadius="2dp" 
    card_view:cardUseCompatPadding="true"> 

    <RelativeLayout 
     android:id="@+id/footerView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:padding="10dp"> 

     <android.support.v7.widget.AppCompatTextView 
      android:id="@+id/txtListTitle" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="HMS Scrap" 
      android:textColor="@color/colorWhite" /> 

     <android.support.v7.widget.AppCompatTextView 
      android:id="@+id/txtListName" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/txtListTitle" 
      android:layout_marginTop="10dp" 
      android:text="HMS Scrap" 
      android:textColor="@color/colorWhite" /> 

    </RelativeLayout> 

</android.support.v7.widget.CardView> 

Nun schreibe ich Adapter Dateicode

adapter.java

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> { 
    private Context mContext; 
    private List<ListModal> listTitle; 

    public ListAdapter(Context mContext, List<ListModal> listTitle) { 
     this.mContext = mContext; 
     this.listTitle = listTitle; 
    } 

    @Override 
    public ListAdapter.ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.raw_list, parent, false); 

     return new ListAdapter.ListViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(ListAdapter.ListViewHolder holder, int position) { 
     ListModal objListModal = listTitle.get(position); 
     holder.txtListTitle.setText(objListModal.getList_name()); 
     holder.txtListTitle.setText(objListModal.getList_title()); 
    @Override 
    public int getItemCount() { 
     return 0; 
    } 

    public class ListViewHolder extends RecyclerView.ViewHolder { 
     TextView txtListTitle, txtListName; 
     CardView cvList; 
     public ListViewHolder(View itemView) { 
      super(itemView); 

      txtListTitle = (TextView) itemView.findViewById(R.id.txtListTitle); 
      txtListName = (TextView) itemView.findViewById(R.id.txtListName); 
      cvList = (CardView) itemView.findViewById(R.id.cardView); 
     } 
    } 
} 

Jetzt Fragment.java

RecyclerView rvDisplay; 
private ListAdapter adapter; 
private List<ListModal> lstmodal; 

public CreateListFragment() { 
    // Required empty public constructor 
} 

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

    View view = inflater.inflate(R.layout.fragment_create_list, container, false); 

    rvDisplay = (RecyclerView) view.findViewById(R.id.rvDisplay); 

    lstmodal = new ArrayList<>(); 

    final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
    rvDisplay.setLayoutManager(layoutManager); 
    makeJsonArrayRequest(); 
    return view; 
} 

private void makeJsonArrayRequest() { 
    String cancel_req_tag = "list"; 

    JsonArrayRequest req = new JsonArrayRequest(URL_FOR_SELECT, 
      new Response.Listener<JSONArray>() { 
     @Override 
     public void onResponse(JSONArray response) { 
      Log.d("OnResponse", response.toString()); 

      try { 
       // Parsing json array response 
       // loop through each json object 
       for (int i = 0; i < response.length(); i++) { 

        JSONObject jsonObject = response.getJSONObject(i); 
        ListModal objListModal = new ListModal(); 
        if (!jsonObject.isNull("list_name")) { 
         objListModal.list_name = jsonObject.getString("list_name"); //here we can fetch webservice field 
        } 
        if (!jsonObject.isNull("list_title")) { 
         objListModal.list_title = jsonObject.getString("list_title"); //here we can fetch webservice field 
        } 

        lstmodal.add(i, objListModal); 
       } 

       adapter = new ListAdapter(getActivity(), lstmodal); 
       rvDisplay.setItemAnimator(new DefaultItemAnimator()); 
       rvDisplay.setAdapter(adapter); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
       Toast.makeText(getActivity(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
      } 

     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyLog.d("VolleyError", "Error: " + error.getMessage()); 
      Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

Hier habe ich diesen Code habe ich versucht, diese Störung erhalte bitte Arbeitsprotokoll

enter image description here

+0

siehe diese http://www.androidhive.info/2016/01/android-working-with-recycler-view/ oder diese http://www.vogella.com/tutorials/AndroidRecyclerView/article .html –

+0

Um Ihnen eine gute Antwort zu geben, könnte es uns helfen, wenn Sie einen Blick auf [fragen] haben, falls Sie es nicht bereits getan haben. Es könnte auch nützlich sein, wenn Sie ein [mcve] bereitstellen könnten. – Mat

Antwort

0

In Ihrem ersten Code-Block sagen Sie die Layoutdatei fragment.xml jedoch im Fragment-Code genannt wird Sie anrufen sehen inflater.inflate(R.layout.fragment_create_list, ...)

Stellen Sie sicher, dass Sie die gleiche Layoutdatei wie das Fragment aufblasen.

+0

Ja, ich blase die gleiche Layout-Datei, aber ich bekomme einen Fehler in setLayoutManager @Mauin –

+0

versuchen, Ihren Code unter onActivityCreated-Methode setzen –

0
@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    rvDisplay = (RecyclerView) view.findViewById(R.id.rvDisplay); 

    lstmodal = new ArrayList<>(); 

    final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
    rvDisplay.setLayoutManager(layoutManager); 
    makeJsonArrayRequest(); 
} 
Verwandte Themen