gpt4 book ai didi

java - 空对象引用上的 Item Click.onItemClick(int) 异常?

转载 作者:行者123 更新时间:2023-12-04 16:35:27 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





What is a NullPointerException, and how do I fix it?

(12 个回答)


2年前关闭。




我已经在我的项目中使用 view-pager 实现了图像 slider 。现在,当我单击 slider 的特定图像时,我想要。它将打开一个新的细节 fragment 。根据该图像 slider 信息在哪里可用。所以我尝试通过使用 setOnClickListner 获取图像位置来实现接口(interface)。但是现在的问题是当我单击图像时出现异常。我的应用程序崩溃了。下面是我的代码...

slider 图像适配器:

public class SliderImageAdapter extends SliderViewAdapter<SliderImageAdapter.SliderAdapterVH>{

public List<Banner> bannerList;
public Context context;
private OnItemClicked onClick;

public interface OnItemClicked {
void onItemClick(int position);
}

public SliderImageAdapter(Context context, List<Banner> bannerList) {

this.bannerList = bannerList;
this.context = context;
}

@Override
public SliderAdapterVH onCreateViewHolder(ViewGroup parent) {
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_slider_myshop, parent, false);
return new SliderAdapterVH(inflate);
}

@Override
public void onBindViewHolder(final SliderAdapterVH viewHolder, final int position) {

Glide.with(viewHolder.itemView)
.load(bannerList.get(position).getmSliderImage())
.fitCenter()
.into(viewHolder.imageViewBackground);

viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
onClick.onItemClick(position);
}
});
}

@Override
public int getCount() {
return bannerList.size();
}

public int getItemPosition (Object object) {
return POSITION_NONE;
}

public void setOnClick(OnItemClicked onClick) {
this.onClick = onClick;
}



class SliderAdapterVH extends SliderViewAdapter.ViewHolder {

View itemView;
ImageView imageViewBackground;
ImageView imageGifContainer;

public SliderAdapterVH(View itemView) {
super(itemView);
imageViewBackground = itemView.findViewById(R.id.iv_auto_image_slider);
imageGifContainer = itemView.findViewById(R.id.iv_gif_container);
this.itemView = itemView;
}
}
}

在这里,我在 Image Slider Adapter 中遇到异常:java.lang.NullPointerException: Attempt to invoke interface method SliderImageAdapter$OnItemClicked.onItemClick(int)' on an null object reference。
@Override
public void onBindViewHolder(final SliderAdapterVH viewHolder, final int position) {

Glide.with(viewHolder.itemView)
.load(bannerList.get(position).getmSliderImage())
.fitCenter()
.into(viewHolder.imageViewBackground);

viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
onClick.onItemClick(position);
}
});
}

家庭 fragment 代码:
public class HomeFragment extends Fragment implements SliderImageAdapter.OnItemClicked {

private SliderImageAdapter sliderImageAdapter;
private List<Banner> bannerList;

bannerList = new ArrayList<>();
sliderImageAdapter = new SliderImageAdapter(getActivity(), bannerList);

sliderMyshop = view.findViewById(R.id.imageSlider);
sliderMyshop.setSliderAdapter(sliderImageAdapter);
sliderMyshop.setIndicatorAnimation(IndicatorAnimations.WORM); //set indicator animation by using SliderLayout.IndicatorAnimations. :WORM or THIN_WORM or COLOR or DROP or FILL or NONE or SCALE or SCALE_DOWN or SLIDE and SWAP!!
sliderMyshop.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
sliderMyshop.setIndicatorSelectedColor(Color.WHITE);
sliderMyshop.setIndicatorUnselectedColor(Color.GRAY);
sliderMyshop.startAutoCycle();

mFirestore.collection("Banner").orderBy("mSliderImage", Query.Direction.DESCENDING).addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable QuerySnapshot documentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.d(TAG, "Error : " + e.getMessage());
}
assert documentSnapshots != null;

for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

if (doc.getType() == DocumentChange.Type.ADDED) {
String doc_id = doc.getDocument().getId();
Banner banner = doc.getDocument().toObject(Banner.class).withDocId(doc_id);
bannerList.add(doc.getNewIndex(), banner);
sliderImageAdapter.notifyDataSetChanged();

} else if (doc.getType() == DocumentChange.Type.MODIFIED) {
String docID = doc.getDocument().getId();
Banner changedModel = doc.getDocument().toObject(Banner.class).withDocId(docID);
if (doc.getOldIndex() == doc.getNewIndex()) {
// Item changed but remained in same position
bannerList.set(doc.getOldIndex(), changedModel);
} else {
// Item changed and changed position
bannerList.remove(doc.getOldIndex());
bannerList.add(doc.getNewIndex(), changedModel);
}
} else if (doc.getType() == DocumentChange.Type.REMOVED) {
// remove
bannerList.remove(doc.getOldIndex());
}
sliderImageAdapter.notifyDataSetChanged();
}
}
});

@Override
public void onItemClick(int position) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container_dashboard, SliderClickFragment.newInstance(bannerList.get(position).getmSliderImage()));
ft.addToBackStack(null); // this line responsible for adding fragment in backstack.
ft.commit();
}

详细 fragment 代码:
public abstract class SliderClickFragment extends Fragment {

public static Fragment newInstance(String getmSliderImage) {
Bundle args = new Bundle();
HomeFragment fragment = new HomeFragment();
fragment.setArguments(args);
return fragment;
}

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.slider_click_listner, container, false);
return view;
}
}

最佳答案

所以问题可能是您正在调用 OnItemClicked.onClick没有 OnItemClicked 的实例.
快速浏览一下

private OnItemClicked onClick;

public interface OnItemClicked {
void onItemClick(int position);
}

public SliderImageAdapter(Context context, List<Banner> bannerList) {

this.bannerList = bannerList;
this.context = context;
}

然后你在这里使用它
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
onClick.onItemClick(position);
}
});

您正在尝试调用 OnClick null 上的方法 OnItemClicked 的实例.
一种方法是添加 OnItemClicked onclick到构造器作为参数,然后让任何 fragment 实现 OnItemClicked界面。
像这样的东西。
private OnItemClicked onClick;

public interface OnItemClicked {
void onItemClick(int position);
}

public SliderImageAdapter(Context context, List<Banner> bannerList,
OnItemClicked onClick) {

this.bannerList = bannerList;
this.onClick = onClick;
this.context = context;
}

然后在 HomeFragment
public class HomeFragment extends Fragment implements 
SliderImageAdapter.OnItemClicked{
private SliderImageAdapter sliderImageAdapter
= new SliderImageAdapter(getActivity(), bannerList, this);
}

关于java - 空对象引用上的 Item Click.onItemClick(int) 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59701261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com