我使用this解决方案作为向我的RecyclerView添加页脚按钮的指南(最后一个ViewHolder项是一个按钮)但我在我的Adapter类中获得了一个空对象引用#39;当我尝试在按钮上设置onClickListener时, onBindViewHolder 。
我觉得这个问题非常简单,我想我只是在经过多次调试后忽略它。
在设置点击监听器时,您能否发现导致Button mAddChordButton为空的原因?
适配器类
public class UserChordsAdapter extends RecyclerView.Adapter<UserChordsAdapter.ChordViewHolder> {
final private ListItemClickListener mOnClickListener;
private static int viewHolderCount;
private int mNumberItems;
public interface ListItemClickListener {
void onListItemClick(int clickedItemIndex);
}
public UserChordsAdapter(int numberOfItems, ListItemClickListener listener) {
...
}
@Override
public ChordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
...
View listItem;
ChordViewHolder viewHolder;
if (viewType == R.layout.user_chord_list_item) { //item isn't last in list
listItem = inflater.inflate(layoutIdForListItem, parent, shouldAttachToParentImmediately);
viewHolder = new ChordViewHolder(listItem);
viewHolder.viewHolderIndex.setText("Chord " + viewHolderCount);
}
else{ //inflate "+ Add new" button for last list item
listItem = inflater.inflate(layoutIdForListItem, parent, shouldAttachToParentImmediately);
viewHolder = new ChordViewHolder(listItem);
}
viewHolderCount++;
return viewHolder;
}
@Override
public void onBindViewHolder(ChordViewHolder holder, int position) {
if (position == mNumberItems){
holder.mAddChordButton.setOnClickListener(new View.OnClickListener(){ //ERROR HERE
@Override
public void onClick(View v) {
//TODO: Add New Chord into RecyclerView
}
});
}
}
@Override
public int getItemCount() {
return mNumberItems + 1;// +1 is for footer button (add new)
}
class ChordViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
// Will display which ViewHolder is displaying this data
TextView viewHolderIndex;
public Button mAddChordButton;
/**
* Constructor for our ViewHolder. Within this constructor, we get a reference to our
* TextViews and set an onClickListener to listen for clicks. Those will be handled in the
* onClick method below.
*/
public ChordViewHolder(View itemView) {
super(itemView);
mAddChordButton = (Button) itemView.findViewById(R.id.button_add_new); //ATTEMPT TO INITIALIZE BUTTON HERE
viewHolderIndex = (TextView) itemView.findViewById(R.id.tv_view_holder_instance);
itemView.setOnClickListener(this);
}
/**
* Called whenever a user clicks on an item in the list.
* @param v The View that was clicked
*/
@Override
public void onClick(View v) {
int clickedPosition = getAdapterPosition();
mOnClickListener.onListItemClick(clickedPosition);
}
}
/**
* Distinguishes if view is a Chord list item or the last item in the list (add new chord)
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
return (position == mNumberItems) ? R.layout.user_chord_add_new : R.layout.user_chord_list_item;
}
}
logcat的
05-24 22:31:26.284 3083-7540/com.example.foo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.foo, PID: 3083
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.foo.UserChordsAdapter.onBindViewHolder(UserChordsAdapter.java:68)
at com.example.foo.UserChordsAdapter.onBindViewHolder(UserChordsAdapter.java:15)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6067)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6100)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5282)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5158)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1445)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3379)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2906)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1117)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1098)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
at android.view.View.measure(View.java:19734)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:911)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19734)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1358)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:0)
我解决了这个问题。我只是在onCreateViewHolder中夸大了错误的布局。在声明页脚视图的正确布局ID并在我的else语句中对其进行膨胀之后,它工作正常。
正确的代码是:
@Override
public ChordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
int layoutIdForListItem = R.layout.user_chord_list_item;
int layoutIdForFooterItem = R.layout.user_chord_add_new; //SOLUTION
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View listItem;
ChordViewHolder viewHolder;
if (viewType == R.layout.user_chord_list_item) { //inflate normal list item
listItem = inflater.inflate(layoutIdForListItem, parent, shouldAttachToParentImmediately);
viewHolder = new ChordViewHolder(listItem);
viewHolder.viewHolderIndex.setText("Chord " + viewHolderCount);
}
else{ //inflate "+ Add new" button for last list item
listItem = inflater.inflate(layoutIdForFooterItem, parent, shouldAttachToParentImmediately);
viewHolder = new ChordViewHolder(listItem);
}
viewHolderCount++;
return viewHolder;
}