英文:
Builder(android.app.activity) in Builder cannot be applied to anonymous (android.view.View.OnClickListener)
问题
错误信息是:
> Builder(android.app.activity) 中的 Builder 无法应用于匿名 (android.view.View.OnClickListener)。
我试图在信息按钮点击时添加自定义警告对话框,但不幸的是出现了上述错误。
错误出现在以下这一行:new FancyGifDialog.Builder(this)
。
英文:
My error is
> Builder(android.app.activity) in Builder cannot be applied to anonymous (android.view.View.OnClickListener).
I'm trying to add custom alert dialog to be pop up on info button click, but unfortunately getting error above.
public class MyAdapter extends PagerAdapter {
String TAG = "MyAdapter";
Context context;
List<ItemLoader> itemsList;
LayoutInflater layoutInflater;
public MyAdapter(Context context, List<ItemLoader> itemsList) {
this.context = context;
this.itemsList = itemsList;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return itemsList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
((ViewPager)container).removeView((View)object);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View view = layoutInflater.inflate(R.layout.common_fragment,container,false);
ImageView threeDots = view.findViewById(R.id.info_three_dots);
ImageView firebase_image = (ImageView)view.findViewById(R.id.image_firebase);
TextView item_name = (TextView)view.findViewById(R.id.item_name);
TextView item_bid_amount = (TextView)view.findViewById(R.id.item_bid_amount);
Picasso.get().load(itemsList.get(position).getImage()).into(firebase_image);
item_name.setText(itemsList.get(position).getName());
item_bid_amount.setText(itemsList.get(position).getPrice());
threeDots.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new FancyGifDialog.Builder(this)
.setTitle("Granny eating chocolate dialog box")
.setMessage("This is a granny eating chocolate dialog box. This library is used to help you easily create fancy gify dialog.")
.setPositiveBtnText("Ok")
.setPositiveBtnBackground("#FF4081")
.setGifResource(R.drawable.gif1) //Pass your Gif here
.isCancellable(true)
.OnPositiveClicked(new FancyGifDialogListener() {
@Override
public void OnClick() {
Toast.makeText(context,"Ok",Toast.LENGTH_SHORT).show();
}
})
.build();
}
});
container.addView(view);
return view;
}
}
I'm getting error in line new FancyGifDialog.Builder(this)
.
答案1
得分: 0
将这行代码替换为:
new FancyGifDialog.Builder((Activity)context)
您已经在适配器中传递了上下文,所以很简单。
英文:
Replace this line of code :
new FancyGifDialog.Builder(this)
with :
new FancyGifDialog.Builder((Activity)context)
You already have context in adapter pass that simple.
答案2
得分: 0
将以下内容从new FancyGifDialog.Builder(this)
更改为new FancyGifDialog.Builder((Activity)context)
,因为根据错误消息:
Builder(android.app.activity)
in Builder cannot be applied to
anonymous (android.view.View.OnClickListener)
可以清楚地看出,Builder构造函数接受Activity。因此,在onClick(View v)
方法中,当您写new FancyGifDialog.Builder(this)
时,您试图传递OnClickListener的匿名类。
new FancyGifDialog.Builder(this)
这行代码在Activity类中可以正常工作。
英文:
You will have to change line from new FancyGifDialog.Builder(this)
to <br> new FancyGifDialog.Builder((Activity)context)
as from the error
> Builder(android.app.activity)
in Builder cannot be applied to
> anonymous (android.view.View.OnClickListener)
it is clear that Builder constructor accepts Activity. So when in onClick(View v)
method you write new FancyGifDialog.Builder(this)
, you are trying to pass anonymous class of OnClickListener.<br>
new FancyGifDialog.Builder(this)
this line would have worked in the Activity Class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论