我很少看到关于这种情况的相关问题,但找不到任何解决办法,所以我的问题就这样。
我已经列出了包含4个片段的列表,im在frag编号3(catalog)中将数据传递给用户,这个frag为cardview保留recyclerview im尝试为将调用dialogfragment的卡实现onclicklistner,我知道最有效的方法是为viewholder使用接口,我面临一个问题recyclerview适配器构造器。
这是目录碎片:

public class CatalogFragment extends Fragment implements CustomAdapter.OnItemClickListener {

private FloatingActionButton mSharedFab;
private List<MyProducts> productsList;

//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;

private CustomAdapter.OnItemClickListener listener;//////LISTNER FOR DIALOG???

//Volley Request
private RequestQueue requestQueue;

//request counter
private int requestCount = 1;
private static final String URL_INDEX = "http://myserverip/product.php";
//Tag values to read from json
public static final String TAG_IMAGE_URL = "product_img";
public static final String TAG_PRODUCT_SN = "product_serial_num";
public static final String TAG_PRODUCT_TITLE = "product_title";
public static final String TAG_PRODUCT_PRICE = "product_price";
public static final String TAG_PRODUCT_DESCRIPTION = "product_description";

public CatalogFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_catalog, container, false);
    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    layoutManager = new LinearLayoutManager(getActivity().getApplicationContext(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);
    //recyclerView.setHasFixedSize(true);
    RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
    itemAnimator.setAddDuration(1000);
    itemAnimator.setRemoveDuration(1000);
    recyclerView.setItemAnimator(itemAnimator);

    //Initializing our product list
    productsList = new ArrayList<>();
    requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());

    // fetch data
    getData();



    //initialize adapter

    adapter = new CustomAdapter(listener, productsList, getActivity());
    //Adding adapter to recyclerview
    recyclerView.setAdapter(adapter);
    return view ;
}
 @Override
public void onItemClicked(View v) {
    DialogAddToCartFragment df= new DialogAddToCartFragment();
    df.show(getFragmentManager(), "Dialog");
}

适配器用3个参数实例化,使用customadapter。
CustomAdapter类:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {

public interface OnItemClickListener {
    void onItemClicked(View v);
}

private OnItemClickListener listener;
//Image loader inst for image
private ImageLoader imageLoader;

private Context context;
public Snackbar snackbar;
String title;
//List  all products
List<MyProducts> myProducts;

public CustomAdapter( OnItemClickListener listener, List<MyProducts> myProducts, Context context)
{
    super();
    this.listener = listener;
    this.myProducts = myProducts;
    this.context = context;
}

@Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.product_list, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) {

    if( myProducts != null) {
        MyProducts myProducts1 = myProducts.get(position);
        imageLoader = ImageVolley.getInstance(context).getImageLoader();
        imageLoader.get(myProducts1.getProductImage(), ImageLoader.getImageListener(holder.imageView, R.drawable.android_store_log, android.R.drawable.ic_dialog_alert));
        title = myProducts1.getProductTitle();
        //Showing data to the views
        holder.imageView.setImageUrl(myProducts1.getProductImage(), imageLoader);
        holder.textViewProductTitle.setText(myProducts1.getProductTitle());
        holder.textViewProductDescription.setText(myProducts1.getProductDescription());
        holder.textViewProductSerialNumber.setText(myProducts1.getProductSn());
        holder.textViewProductPrice.setText(myProducts1.getProductPrice());
    }
}

@Override
public int getItemCount() {
    return myProducts.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
    //Views
    public NetworkImageView imageView;
    public TextView textViewProductTitle;
    public TextView textViewProductDescription;
    public TextView textViewProductSerialNumber;
    public TextView textViewProductPrice;

    //Initializing Views
    public ViewHolder(final View itemView) {
        super(itemView);
        imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewProduct);
        textViewProductTitle = (TextView) itemView.findViewById(R.id.textViewProductTitle);
        textViewProductDescription = (TextView) itemView.findViewById(R.id.textViewProductDescription);
        textViewProductSerialNumber = (TextView) itemView.findViewById(R.id.textViewProductSerialNumber);
        textViewProductPrice = (TextView) itemView.findViewById(R.id.textViewProductPrice);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClicked(v);/////////DIALOG LISTENER????
            }
        });
    }
}

对话片段:
public class DialogAddToCartFragment extends DialogFragment {



@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog_add_to_cart, new LinearLayout(getActivity()), false);

    // Retrieve layout elements
    //TextView title = (TextView) view.findViewById(R.id.text_title);

    // Set values
    //title.setText("Not perfect yet");

    // Build dialog
    Dialog builder = new Dialog(getActivity());
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.getWindow().setBackgroundDrawable(new ColorDrawable(Color.GREEN));
    builder.setContentView(view);
    return builder;

}

}
当我点击卡片时,程序关闭:(
这是日志:
07-12 08:42:22.685 2808-2808/com.example.get2i.androidstore E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.get2i.androidstore, PID: 2808
                                                                          java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.get2i.androidstore.CustomAdapter$OnItemClickListener.onItemClicked(android.view.View)' on a null object reference
                                                                              at com.example.get2i.androidstore.CustomAdapter$ViewHolder$1.onClick(CustomAdapter.java:102)
                                                                              at android.view.View.performClick(View.java:5198)
                                                                              at android.view.View$PerformClick.run(View.java:21147)
                                                                              at android.os.Handler.handleCallback(Handler.java:739)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我应该用什么来指示catalogfrag中的customadapter.onitemclicklistener侦听器?如有任何建议,欢迎垂询,谢谢!

最佳答案

我应该如何说明customadapter.onitemclicklistener
目录框中的侦听器?
因为在将对象传递给类构造函数之前忘记初始化它。按以下方式操作:

listener=this;
adapter = new CustomAdapter(listener, productsList, getActivity());
...

10-06 07:09