I want to build custom adapter for RecyclerView but when I set Text or set value for any view it's return NullPointerException. I don't why. Tried many solution but all of them didn't work. My adapter:
public class HistoryViewAdapter extends RecyclerView.Adapter<HistoryViewHolder> {
private ArrayList<ApplicationDataManager> arrayOfHistoryManagerClass;
public HistoryViewAdapter(ArrayList<ApplicationDataManager> HistoryClassArray) {
this.arrayOfHistoryManagerClass = HistoryClassArray;
}
@Override
public HistoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.history_translate_include, parent, false);
return new HistoryViewHolder(itemView);
}
@Override
public void onBindViewHolder(HistoryViewHolder holder, int position) {
ApplicationDataManager managerClass = arrayOfHistoryManagerClass.get(position);
holder.NonTranslatedWord.setText(managerClass.getNonTranslatedText());
holder.TranslatedWord.setText(managerClass.GetTranslatedText());
holder.isFavoret.setChecked(managerClass.GetFavoredStat());
}
@Override
public int getItemCount() {
return arrayOfHistoryManagerClass.size();
}
}
My ViewHolder (I tried FindViewByid instead of Butterknife but it gives the same Exception):
public class HistoryViewHolder extends RecyclerView.ViewHolder {
// Defining a TextView
@InjectView(R.id.translated_word)
protected TextView TranslatedWord;
// Defining a TextView
@InjectView(R.id.non_translated_word)
protected TextView NonTranslatedWord;
@InjectView(R.id.history_checkBox)
protected CheckBox isFavoret;
public HistoryViewHolder(View itemView) {
super(itemView);
//injecting views
ButterKnife.inject(itemView);
}
}
Finally MyFragment:
public class HistoryFragment extends Fragment {
@InjectView(R.id.history_recyclerview)
RecyclerView historyRecycler;
HistoryViewAdapter historyViewAdapter;
ArrayList<ApplicationDataManager> listDataManager;
LinearLayoutManager layoutManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.history_translate_include, container, false);
ButterKnife.inject(this, rootView);
layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
historyRecycler.setLayoutManager(layoutManager);
listDataManager = new ArrayList<ApplicationDataManager>();
listDataManager.add(new ApplicationDataManager("Test","Test",true,"Engilsh","arabic"));
listDataManager.add(new ApplicationDataManager("Test","Test",false,"Engilsh","arabic"));
historyViewAdapter = new HistoryViewAdapter(listDataManager);
historyRecycler.setAdapter(historyViewAdapter);
return rootView;
}
}
UPDATE: My logcat:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at list.adapter.HistoryViewAdapter.onBindViewHolder(HistoryViewAdapter.java:42)
at list.adapter.HistoryViewAdapter.onBindViewHolder(HistoryViewAdapter.java:18)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4138)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3448)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1988)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.widget.ScrollView.onLayout(ScrollView.java:1531)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1705)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1559)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1705)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1559)
at android.widget.LinearLayout.onLayout(LinearLayout.j
My history_translate_include.xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/history_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:text="@string/translate_history"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RecyclerView
android:id="@+id/history_recyclerview"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_alignLeft="@+id/history_textview"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignStart="@+id/history_textview"
android:layout_below="@+id/history_textview"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
/>
</RelativeLayout>
</ScrollView>
Thanks in advance
You use wrong ButterKnife inject method.
Use HistoryViewHolder
constructor below:
public HistoryViewHolder(View itemView) {
super(itemView);
//This line is important
ButterKnife.inject(this,itemView);
}
instead of
public HistoryViewHolder(View itemView) {
super(itemView);
//injecting views
ButterKnife.inject(itemView);
}
When you want to inject some views for object which contains these views but it is not Fragment
Activity
or View
, you have to call method:
ButterKnife.inject(Object,View);
And additionally you use history_translate_include
in onCreateViewHolder method, you should use there layout for single item which contains view with these ids : translated_word, non_translated_word, history_checkBox
@Override
public HistoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.layout_for_single_item, parent, false);
return new HistoryViewHolder(itemView);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With