我不明白为什么它给我nullPointerException如果在我看来一切都很好

时间:2017-04-06 16:37:21

标签: android nullpointerexception togglebutton

大家好,我有这个例外非常常见,但我不明白为什么它给了我一切对我来说是好的我不知道出了什么问题

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);
                    }


                }
            });

并在RecyclerHolder中捕获id:

    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);




    }
 }

1 个答案:

答案 0 :(得分:0)

Find the error, the exception gave me why in the findViewById (); I was referring to a ToggleButton that is located in a Video Layout and not a normal Post