Я создаю представление Recycler во фрагменте, при запуске моего приложения приложения автоматически закрываются и показывают следующую ошибку:
При использовании макета фрагмента он показывает ошибку, за исключением того, что при использовании пустогомакет, процесс работает нормально и легко запускается.
Process: com.example.lenovo.skanda, PID: 9018
> java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.lenovo.skanda.Listitem.getHead()' on a
> null object reference
> at `enter code here`com.example.lenovo.skanda.MyAdapter.onBindViewHolder(MyAdapter.java:35)
> at com.example.lenovo.skanda.MyAdapter.onBindViewHolder(MyAdapter.java:12)
Нужна помощь .. !!
Fragment_stories.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:padding="10dp"
android:layout_margin="@dimen/cardview_default_elevation"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textViewHead"
android:text="Heading"
android:textSize="15dp"
android:gravity="center"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewDesc"
android:text="Description"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
StoriesFragment.java
public class StoriesFragment extends Fragment {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<Listitem> listitems;
String[] head = new String[]{"apple", "banana", "curry", "dog"};
String[] desc = new String[]{"This is article 1", "This is article 2", " This is article 3”, "this is article 4"};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_stories, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
listitems = new ArrayList<>();
for (int i = 0; i <= 3; i++) {
new Listitem(
head[i],
desc[i]);
Listitem listitem = null;
listitems.add(listitem);
}
adapter = new MyAdapter(listitems, getActivity());
recyclerView.setAdapter(adapter);
return view;
}
}
Listitem.java
package com.example.lenovo.skanda;
public class Listitem {
private String head;
private String desc;
public Listitem(String head, String desc) {
this.head = head;
this.desc = desc;
}
public String getHead() {
return head;
}
public String getDesc() {
return desc;
}
}
MyAdapter.java
package com.example.lenovo.skanda;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<Listitem> listitems;
private Context context;
public MyAdapter(List<Listitem> listitems, Context context) {
this.listitems = listitems;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Listitem listitem = listitems.get(position);
holder.textViewHead.setText(listitem.getHead());
holder.textViewDesc.setText(listitem.getDesc());
}
@Override
public int getItemCount() {
return listitems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView textViewHead;
public TextView textViewDesc;
public ViewHolder(View itemView) {
super(itemView);
textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);
}
}
}