-2
私はRecyclerView(最後のViewHolderアイテムはボタンです)にフッターボタンを追加するためのガイドとしてthisソリューションを使用していますが、私はnullオブジェクトリファレンスを取得していますアダプタクラスのonBindViewHolderボタンにonClickListenerを設定しようとしました。RecyclerViewによるクラッシュヌルリファレンスのsetOnClickListenerボタン
私は問題がかなりシンプルだと感じています。私は多くのデバッグの後でそれを見落としていると思います。
オンクリックリスナーを設定すると、ボタンmAddChordButtonがnullになる原因を突き止めることができますか?
アダプタクラス
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)
logcat –
ポストフルLogcatを追加してください。 –
この例を見てください。https://github.com/vkdinventor/RecycleViewLoadMore –