我正在使用适配器将视图从Firebase填充到CardView。我正在尝试使用onclicklistener在适配器内的cardView上打开一个新片段。它曾经在我不使用片段时工作,打开Activity2,如下所示:
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));
}
});
}
现在在使用片段时,我尝试使用适配器中的viewpager来选择卡片时的另一个视图,但这不起作用。我每次启动应用程序时都会点击卡片崩溃:
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);
}
});
}
这是我的崩溃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)
提前感谢您的帮助。
答案 0 :(得分:1)
考虑到你的片段中有你的视图寻呼机。
您的ViewPager未初始化,初始化Viewpager的方式不正确。
您可以使用界面执行此操作。
创建界面
public interface ViewPagerListener{
void setViewPager(int position);
}
在Fragment中实现界面
public class MyFragment extends Fragment implements ViewPagerListener{
//method to be implemented.
public void setViewPager(int position){
//TODO: do your implementation here
}
}
在其中一个构造函数
中的适配器中添加ViewPagerListener
param
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;
}
}
在你的片段中,像这样初始化你的适配器
MyAdapter myAdapter = new MyAdapter(getActivity(),objects,MyFragment.this); //3rd param is your interface instance.
调用适配器onBindViewHolder
方法
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
}
});
}
您可以像这样设置片段中的ViewPager位置
public void setViewPager(int position){
viewPager.setCurrentItem(position);
}