我想为RecyclerView构建自定义适配器,但是当我为任何视图设置Text或设置值时,它返回NullPointerException。我不是为什么。尝试了许多解决方案,但所有这些都没有奏效。 我的适配器:
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();
}
}
我的ViewHolder(我试过FindViewByid而不是Butterknife,但是它给出了相同的异常):
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);
}
}
最后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;
}
}
更新: 我的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
我的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>
提前致谢
答案 0 :(得分:8)
你使用错误的ButterKnife注入方法。
使用下面的HistoryViewHolder
构造函数:
public HistoryViewHolder(View itemView) {
super(itemView);
//This line is important
ButterKnife.inject(this,itemView);
}
而不是
public HistoryViewHolder(View itemView) {
super(itemView);
//injecting views
ButterKnife.inject(itemView);
}
如果要为包含这些视图的对象注入一些视图但不是Fragment
Activity
或View
,则必须调用方法:
ButterKnife.inject(Object,View);
此外,您在onCreateViewHolder方法中使用history_translate_include
,您应该使用包含这些ID的视图的单个项目布局: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);
}