I'm using an adapter to populate view from Firebase to a CardView. I'm trying to open a new fragment with onclicklistener on an item from the cardView inside the adapter. It used to work when I was not using fragments, to open Activity2 as shown below:
public void onBindViewHolder(ViewHolder holder, int position) {
Upload upload = (Upload) this.uploads.get(position);
holder.textViewName.setText(upload.getName());
Glide.with(this.context).load(upload.getUrl()).into(holder.imageView);
holder.textViewYear.setText(upload.getYr());
holder.mView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.getContext().startActivity(new Intent(v.getContext(), Activity2.class));
}
});
}
Now while using fragments, I tried with viewpager inside my adapter to select another view when the card is clicked, which doesn't work. Every time I launch the app it crashes with click on the card:
public void onBindViewHolder(ViewHolder holder, int position) {
Upload upload = (Upload) this.uploads.get(position);
holder.make.setText(upload.getName());
Glide.with(this.context).load(upload.getUrl()).into(holder.imageView);
holder.year.setText(upload.getYr());
holder.mView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
viewPager.setCurrentItem(3);
}
});
}
This is my crash logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: greeny.cop, PID: 22602
java.lang.NullPointerException
at greeny.cop.Adapter.CarPath$1.onClick(CarPath.java:61)
at android.view.View.performClick(View.java:4446)
at android.view.View$PerformClick.run(View.java:18430)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5281)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:932)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:748)
at dalvik.system.NativeStart.main(Native Method)
Thanks in advance for any help.
Considering you have your view pager in your Fragment.
Your ViewPager is not initialized and the way you have initialized your Viewpager is incorrect.
You could use an interface to do this.
Create an interface
public interface ViewPagerListener{
void setViewPager(int position);
}
Implement the interface in your Fragment
public class MyFragment extends Fragment implements ViewPagerListener{
//method to be implemented.
public void setViewPager(int position){
//TODO: do your implementation here
}
}
Add a ViewPagerListener
param in your adapter in one of the constructor
public class MyAdapter extends ArrayAdapter<Obj>{
Context c;
ArrayList<Obj> objects
ViewPagerListener mListener;
public MyAdapter(Context c,ArrayList<Obj> objects,ViewPagerListener mListener){
this.c = c;
this.objects = objects;
this.mListener = mListener;
}
}
In your Fragment, initialize your Adapter like this
MyAdapter myAdapter = new MyAdapter(getActivity(),objects,MyFragment.this); //3rd param is your interface instance.
Call the interface method in your adapter onBindViewHolder
method
public void onBindViewHolder(ViewHolder holder, int position) {
Upload upload = (Upload) this.uploads.get(position);
holder.make.setText(upload.getName());
Glide.with(this.context).load(upload.getUrl()).into(holder.imageView);
holder.year.setText(upload.getYr());
holder.mView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mListener.setViewPager(3) // Calling the interface method
}
});
}
You can set the ViewPager position in your fragment like this
public void setViewPager(int position){
viewPager.setCurrentItem(position);
}