【发布时间】:2019-05-25 11:16:43
【问题描述】:
以下代码显示RecyclerView 类和底层活动是选项卡布局。现在代码的方式是我收到一个错误,我声明AlertDialog 如果我使用上下文,则错误显示Builder cannot be applied to the Class 应用程序崩溃。我之前使用过相同的代码,但使用了 Firestore Recycler Adapter 和 TabLayout。这个问题不是重复的,null pointer java/lang/nullpointerexception 太笼统了。我都经历过。
public class RecyclerAdapter extends FirestoreRecyclerAdapter<Items,
RecyclerAdapter.MyViewHolder> {
Context context;
public RecyclerAdapter(@NonNull FirestoreRecyclerOptions<Items> options)
{
super(options);
}
@Override
protected void onBindViewHolder(@NonNull MyViewHolder holder, final int
position, @NonNull final Items model) {
holder.name.setText(model.getName());
holder.category.setText(model.getCategory());
holder.price.setText(model.getPrice());
holder.manu.setText(model.getManufacturer());
Picasso.get()
.load(model.getImage())
.fit()
.centerCrop()
.into(holder.Thumbnail);
holder.addtocart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAddToCartDialog(model);
}
});
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
View view;
LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
view = mInflater.inflate(R.layout.activity_adapter, parent, false);
return new MyViewHolder(view);
}
class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView category;
public ImageView Thumbnail;
public TextView price;
public TextView manu;
public Button addtocart;
public MyViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.rowname);
category = itemView.findViewById(R.id.categorie);
Thumbnail = itemView.findViewById(R.id.thumbnail);
price = itemView.findViewById(R.id.price);
manu = itemView.findViewById(R.id.manu);
addtocart = itemView.findViewById(R.id.cartbutton);
}
}
public void showAddToCartDialog(final Items items) {
View view;
AlertDialog.Builder builder = new
AlertDialog.Builder(RecyclerAdapter.this);
LayoutInflater lflator = LayoutInflater.from(context);
view = lflator.inflate(R.layout.add_to_cart_layout, null);
ImageView image_product_dialogue =
view.findViewById(R.id.image_cart_product);
TextView textView_product_dialogue =
view.findViewById(R.id.text_cart_product);
RadioButton nairobi = view.findViewById(R.id.radio_nairobi);
RadioButton mombasa = view.findViewById(R.id.radio_mombasa);
RadioButton kisumu = view.findViewById(R.id.radio_kisumu);
RadioButton nakuru = view.findViewById(R.id.radio_nakuru);
RadioButton express = view.findViewById(R.id.radio_ex);
RadioButton tomorow = view.findViewById(R.id.radio_tomo);
final ElegantNumberButton text_count =
view.findViewById(R.id.text_count);
express.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
Items.delivery = 0;
}
}
});
tomorow.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked)
Items.delivery = 1;
}
});
nairobi.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked)
Items.location = 0;
}
});
mombasa.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked)
Items.location = 1;
}
});
kisumu.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked)
Items.location = 2;
}
});
nakuru.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked)
Items.location = 3;
}
});
Picasso.get()
.load(items.getImage())
.fit()
.centerCrop()
.into(image_product_dialogue);
textView_product_dialogue.setText(items.getName());
builder.setView(view);
builder.setNegativeButton("ADD TO CART", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// showConfirmDialogue(position, text_count.getNumber(),
text_count.getNumber());
dialog.dismiss();
}
});
builder.show();
}
}
【问题讨论】:
-
@Stultuske 不,这太笼统了。
-
NPE 只有一个原因,该链接中对此进行了解释。你没有向我们展示任何堆栈跟踪,你没有告诉我们在哪一行引发了异常......这并不是说我们可以在这里运行你的代码。如果您不理解该链接中的说明,也许您应该在尝试创建应用程序之前关注基础知识
-
@Stultuske 它不会抛出任何东西,它无法编译它在
AlertDialog.Builder builder = new AlertDialog.Builder(RecyclerAdapter.this)下显示Builder (android.content.Context) in Builder cannot be applied to (com.example.navigationdrawer.RecyclerAdapter)如果我使用上下文,它只会抛出空指针错误。 -
为什么要从适配器打开对话框?
-
@JainilPatel 一个好问题,我这样做的原因是它通常对我有用,除了今天。建议将不胜感激。