【问题标题】:I am trying to open Dialog but I keep on getting compilation error of `Builder (android.content.Context) in Builder cannot be applied to the Activity [duplicate]我正在尝试打开 Dialog 但我不断收到 Builder 中的 `Builder (android.content.Context) 的编译错误无法应用于 Activity [重复]
【发布时间】:2019-05-25 11:16:43
【问题描述】:

以下代码显示RecyclerView 类和底层活动是选项卡布局。现在代码的方式是我收到一个错误,我声明AlertDialog 如果我使用上下文,则错误显示Builder cannot be applied to the Class 应用程序崩溃。我之前使用过相同的代码,但使用了 Firestore Recycler AdapterTabLayout。这个问题不是重复的,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 一个好问题,我这样做的原因是它通常对我有用,除了今天。建议将不胜感激。

标签: java android firebase


【解决方案1】:

在构造函数中传递活动上下文并使用该上下文创建对话框。类似的东西..

public RecyclerAdapter(@NonNull FirestoreRecyclerOptions<Items> options,Activity context) 
{
    super(options);
    this.context=context;
}

【讨论】:

  • 相同的空指针表达式
  • 嘿,我在我的基础活动中使用 getActivity 而不是 getApplicationContext
【解决方案2】:

检查此答案(与您的问题相同) Android | AlertDialog on RecyclerView Item Click

正如其他答案中提到的,您可以将上下文传递给适配器构造函数,我个人认为最好不要以这种方式传递活动上下文(可能会出现一些内存泄漏), 所以另一种方法是创建 ClickListener 接口并将此事件(可能使用数据作为方法参数)传递给活动/片段,您将在其中处理点击并创建 AlertDialog。

【讨论】:

    【解决方案3】:

    只需用这个替换您的适配器类代码 -

    public class RecyclerAdapter extends FirestoreRecyclerAdapter<Items, 
    RecyclerAdapter.MyViewHolder> {
    
    Context context;
    
    
    public RecyclerAdapter(@NonNull FirestoreRecyclerOptions<Items> options, Context context) 
    {
        super(options);
        this.context = context;
    }
    
    @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(context);
        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();
    
      }
    
     }
    

    然后调用创建您的适配器类对象,如下所示 -

    RecyclerAdapter adapter = new RecyclerAdapter(options, YourActivity.this); // If you are creating adapter class object from fragment then replace `YourActivity.this` with `getActivity()`.
    recyclerView.setAdapter(adapter);
    

    【讨论】:

    • 相同的空指针表达式
    • 嘿,我在我的基础活动中使用 getActivity 而不是 getApplicationContext
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多