-3
大家好我有這個例外是非常普遍的,但我不明白爲什麼它給了我,如果一切都很好我的觀點我不知道什麼是錯我不明白爲什麼它給了我nullPointerException,如果我認爲一切都很好
的exepction:
Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.ToggleButton.isChecked()' on a null object reference
at com.android.octa.memetixs.ui.fragment_spanish.fragrandom.PostFragment$2.populateViewHolder(PostFragment.java:163)
at com.android.octa.memetixs.ui.fragment_spanish.fragrandom.PostFragment$2.populateViewHolder(PostFragment.java:91)
這種方法是在PopulateviewHolder
viewHolder.shakaLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(viewHolder.shakaLike.isChecked()) {
Drawable myLike = getResources().getDrawable(R.drawable.likedefinitivo);
viewHolder.shakaLike.setBackgroundDrawable(myLike);
Drawable myLikeM = getResources().getDrawable(R.drawable.middleblack);
viewHolder.middleFinger.setBackgroundDrawable(myLikeM);
onLikeClick(model.getPostId());
}else if (!viewHolder.shakaLike.isChecked()){
Drawable m = getResources().getDrawable(R.drawable.shakasign);
viewHolder.shakaLike.setBackgroundDrawable(m);
}
}
});
和ID是在RecyclerHolder捕獲:
public static class PostHolder extends RecyclerView.ViewHolder {
ToggleButton shakaLike;
ToggleButton middleFinger;
public PostHolder(View itemView) {
super(itemView);
shakaLike = (ToggleButton) itemView.findViewById(R.id.tg_likeVideo);
middleFinger = (ToggleButton) itemView.findViewById(R.id.tg_dislikeVideo);
}
}
這可能是因爲您試圖在PostHolder之外使用一個偵聽器,在這裏您是配偶在PostHolder中使用 – MohammedAlSafwan
這在一個PopulateViewHolder @MohammedAlSafwan'保護無效populateViewHolder(最終PostHolder viewHolder,最終Post模型,int位置){}' – madmvx
@madmvx你會把它在你的文章,看看你在哪裏使用它。 – MohammedAlSafwan