-1

Ich habe eine Registerkarte Aktivität, ich möchte in dem ersten Fragment (Tab1History) eine Recyclerview und Cardview zeigen. Ich habe eine XML-Datei für CardView erstellt, und im XML-Fragment habe ich die Recyclerview eingefügt. Ich habe erstellt auch den Adapter:"RecyclerView.setHasFixedSize (boolean)" auf eine Null-Objekt-Referenz "Recyclerview in Fragment

public class CespiteAdapter extends RecyclerView.Adapter<CespiteAdapter.ViewHolder> 
{ 
    private List<CespiteOgg> cespiteOggList; 
    private Context context; 

public CespiteAdapter(List<CespiteOgg> cespiteOggList, Context context) { 
    this.cespiteOggList = cespiteOggList; 
    this.context = context; 
} 

public class ViewHolder extends RecyclerView.ViewHolder 
{ 
    public CardView cv; 
    public TextView txtNumInventario; 
    public TextView txtNomeCespite; 
    public TextView txtDtCatalogazione; 
    public TextView txtAula; 
    public TextView txtNomeUser; 


    ViewHolder(View itemView) 
    { 
     super (itemView); 
     //cv = (CardView) itemView.findViewById(R.id.cardView); 
     txtNumInventario = (TextView) itemView.findViewById(R.id.txtNumeroInventario); 
     txtNomeCespite = (TextView) itemView.findViewById(R.id.txtNomeCespite); 
     txtDtCatalogazione = (TextView) itemView.findViewById(R.id.txtDataCatalogazione); 
     txtAula = (TextView) itemView.findViewById(R.id.txtAula); 
     txtNomeUser= (TextView) itemView.findViewById(R.id.txtNomeUser); 

    } 
} 



@Override 
public CespiteAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{ 
    LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 

    View cespiteView = inflater.inflate(R.layout.cespite_card_view, parent, false); 

    return new ViewHolder(cespiteView); 
} 

@Override 
public void onBindViewHolder(ViewHolder holder, int position) 
{ 
    CespiteOgg cespiteOgg = cespiteOggList.get(position); 

    holder.txtNumInventario.setText(cespiteOgg.getNumInventario()); 
    holder.txtNomeCespite.setText(cespiteOgg.getNomeCespite()); 
    holder.txtDtCatalogazione.setText(cespiteOgg.getDtCatalogazione().toString()); 
    holder.txtAula.setText(cespiteOgg.getAula()); 
    holder.txtNomeUser.setText(cespiteOgg.getNomeUser()); 

} 

@Override 
public int getItemCount() 
{ 
    return cespiteOggList.size(); 
} 
} 

Und hier ist das Fragment:

public class Tab1History extends Fragment 
{ 



private RecyclerView recyclerView; 
    private RecyclerView.Adapter adapter; 

private List<CespiteOgg> cespiteOggList; 
private Date location; 
Date date = new Date(location.getTime()); 



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

    recyclerView = (RecyclerView) getView().findViewById(R.id.RecyclerView); 
    recyclerView.setHasFixedSize(true);//every item of the RecyclerView has a fix size 
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 

    cespiteOggList = new ArrayList<>(); 

    for(int i=0; i<=10; i++) 
    { 
     CespiteOgg cespiteOgg = new CespiteOgg("heading"+(i+1), "Lorem ipsum", date, "sdjijsad", "jdkjd"); 

     cespiteOggList.add(cespiteOgg); 

    } 

    adapter = new CespiteAdapter(cespiteOggList, getContext()); 

    recyclerView.setAdapter(adapter); 

      return rootView; 
} 
} 

Und es ist der Stacktrace:

FATAL EXCEPTION: main 
                     Process: com.example.arslan.qrcode, PID: 14311 
                     java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference 

Hier cespite_card_view XML:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/cardView"> 


<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/imgCespite" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginRight="16dp" 
     android:src="@mipmap/thebigger" /> 

    <TextView 
     android:id="@+id/txtNumeroInventario" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@id/imgCespite" 
     android:text="text1" /> 

    <TextView 
     android:id="@+id/txtNomeCespite" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/txtNumeroInventario" 
     android:layout_toRightOf="@id/imgCespite" 
     android:text="text2" /> 

    <TextView 
     android:id="@+id/txtDataCatalogazione" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/txtNomeCespite" 
     android:layout_toRightOf="@id/imgCespite" 
     android:text="text3" /> 


    <TextView 
     android:id="@+id/txtAula" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/txtDataCatalogazione" 
     android:layout_toRightOf="@id/imgCespite" 
     android:text="text4" /> 

    <TextView 
     android:id="@+id/txtNomeUser" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/txtAula" 
     android:layout_toRightOf="@id/imgCespite" 
     android:text="text5" /> 

</RelativeLayout> 

Und es ist das Fragment-Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.arslan.qrcode.Main" 
android:layout_weight="1" 
> 

<android.support.v7.widget.RecyclerView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/my_recycler_view"></android.support.v7.widget.RecyclerView> 

</RelativeLayout> 
+0

Bitte posten Sie Ihr Layout mit dem Namen cespite_card_view –

+0

Sie können 'getView()' nicht in onCreateView aufrufen ... Die Ansicht ist noch nicht erstellt! –

+0

Vermutlich haben sie das aus den Kommentaren zu der letzten Frage, die sie dazu gepostet haben, korrigiert. Das wäre nicht die Stack-Spur, die sie bekommen würden, wenn sie es nicht tun würden. –

Antwort

2

Ändern Sie diese Zeile:

recyclerView = (RecyclerView) getView().findViewById(R.id.RecyclerView); 

Um dies:

recyclerView = (RecyclerView) rootView.findViewById(R.id.RecyclerView); 

Auch verwenden Sie das gleiche Layout für Ihre RecyclerView.Adapter und Ihre Fragment in dieser Zeile:

View rootView = inflater.inflate(R.layout.cespite_card_view, container, false); 

Sie sollten eine Layout-Datei für Ihr Fragment haben, die im Inneren Ihre RecyclerView Widget enthält, wie folgt aus:

<android.support.v7.widget.RecyclerView 
android:id="@+id/recyclerView" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="#e9e9e9 /> 
+0

Hält ab. – arst3k

+0

überprüfen Sie die bearbeitete Antwort jetzt. –

+0

ja im Fragment Layout habe ich das RecyclerView Widget einfügen, aber nicht funktioniert .. – arst3k

0

ändern getView() in rootView

getView - Holen Sie sich für das Layout der Stammansicht des Fragments (das von onCreateView zurückgegebene (LayoutInflater, ViewGroup, Bundle)), falls vorhanden.

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

    recyclerView = (RecyclerView) rootView.findViewById(R.id.RecyclerView); 
    recyclerView.setHasFixedSize(true);//every item of the RecyclerView has a fix size 
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 

    cespiteOggList = new ArrayList<>(); 

    for(int i=0; i<=10; i++) 
    { 
     CespiteOgg cespiteOgg = new CespiteOgg("heading"+(i+1), "Lorem ipsum", date, "sdjijsad", "jdkjd"); 

     cespiteOggList.add(cespiteOgg); 

    } 

    adapter = new CespiteAdapter(cespiteOggList, getContext()); 

    recyclerView.setAdapter(adapter); 

      return rootView; 
} 
+0

stürzt immer wieder ab .. – arst3k

+0

@Arstek Sie keine ID für den RecyclerView festgelegt haben. Bitte überprüfen Sie es –

+0

Nein, ich habe festgelegt, aber ich war die falsche Layout-Datei Aufpumpen gelöst – arst3k

0

Sie sollten Ihre Ansicht aus der aufgeblähten Ansicht finden. Verwendung:

recyclerView = (RecyclerView) rootView.findViewById(R.id.RecyclerView); 

statt:

recyclerView = (RecyclerView) getView().findViewById(R.id.RecyclerView); 

Viel Glück.

+0

stürzt immer wieder ab .. – arst3k

Verwandte Themen