1

i am creating a custom adapter where i am using two view holders, if the list is at position 0 holder object is casted to first view holder otherwise to second. the issue is when i am using that first view holder object outside onBindViewHolder it throws null pointer exception. here is my code

 @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { if (position == 0) { firstholder = (FirstHolder) holder; } else { secondholder = (SecondHolder) holder; } } public void runnable(final int size) { handler = new Handler(); runnable = new Runnable() { @Override public void run() { if (firstholder.ViewPager.getCurrentItem() == size - 1) { firstholder.ViewPager.setCurrentItem(0); } else { firstholder.ViewPager.setCurrentItem(firstholder.ViewPager.getCurrentItem() + 1, true); } handler.removeCallbacks(Runnable); handler.postDelayed(Runnable, 1000); } } }; } 

its throwing exception when called in runnable. Note that: both viewholder objects are global

8
  • Can you post the stack trace? which line is throwing NullPointer? Also post the code for the remainder of the adapter, specifically onCreateViewHolder and getItemViewType methods Commented Jan 4, 2017 at 12:28
  • in run method where i am calling viewpager through firstholder Commented Jan 4, 2017 at 12:30
  • 1
    "Runnable = new Runnable()"? , you should have some variable "Runnable runs=new Runnable()" Commented Jan 4, 2017 at 12:30
  • I think he means here: firstholder.ViewPager.getCurrentItem() Commented Jan 4, 2017 at 12:32
  • yes @Leonardo and btw she not he :P Commented Jan 4, 2017 at 12:32

1 Answer 1

1

try to use this

final int itemType = getItemViewType(position); if (itemType == 0) { firstholder = (FirstHolder) holder; }else { secondholder = (SecondHolder) holder; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.