sujithkanna / SmileyRating

SmileyRating is a simple rating bar for android. It displays animated smileys as rating icon.
Apache License 2.0
1.09k stars 184 forks source link

fails on nullpointerexception when setting a smiley for the first time #32

Closed ghost closed 5 years ago

ghost commented 7 years ago

Your work is awsome! but now I need to make it work :) I follow the 3 steps in the usage part and I see the smilies in the designer preview. I did the following in an adapter: RatingViewHolder(View itemView) { super(itemView); srRating = (SmileRating) itemView.findViewById(R.id.smile_rating); srRating.setIndicator(true); //<< srRating is not null and this line works }

@override public void onBindViewHolder(final RatingViewHolder holder, int position) { Rating theRating = RatingList.get(position); holder.srRating.setSelectedSmile(5,true); << fails* }

Please advice, it seems that this is working for everyone so I wonder if I miss something Thanks

sujithkanna commented 7 years ago

Are you sure it is null pointer? or IndexOutOfBounds exception

sujithkanna commented 7 years ago

Can you post the stackTrace of the error too?

ghost commented 7 years ago

I guess mTouchPoints should be 5 (smilies) but it contains 0 because they were never created. I think. Here is the stacktrace --------- beginning of crash 09-16 08:35:36.497 3992-3992/com.update.update E/AndroidRuntime: FATAL EXCEPTION: main Process: com.update.update, PID: 3992 java.lang.NullPointerException: Attempt to read from field 'float com.hsalf.smilerating.BaseRating$Point.x' on a null object reference at com.hsalf.smilerating.SmileRating$2.onAnimationStart(SmileRating.java:194) at android.animation.ValueAnimator.notifyStartListeners(ValueAnimator.java:1048) at android.animation.ValueAnimator.start(ValueAnimator.java:1110) at android.animation.ValueAnimator.start(ValueAnimator.java:1117) at com.hsalf.smilerating.SmileRating.setSelectedSmile(SmileRating.java:565) at com.hsalf.smilerating.SmileRating.setSelectedSmile(SmileRating.java:545) at com.update.update.DateRatingAdapter.onBindViewHolder(DateRatingAdapter.java:49) -> holder.srRating.setSelectedSmile(5,true);

ghost commented 7 years ago

I hope the above will be enough but if you need more info from me, let me know

yinqingh commented 7 years ago

I got the same error

ghost commented 7 years ago

@LeooonH It seems that we're the only ones to get this error... I wonder what the problem is. Waiting for @sujithkanna response.

BRKNCD commented 6 years ago

Hi all, I want to ask why when I set true for the animation it gives me java.lang.NullPointerException: Attempt to read from field 'float com.hsalf.smilerating.BaseRating$Point.x' on a null object reference

My code (simplyfied):

public class scoreActivity extends AppCompatActivity{

    SmileRating smileRating;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.score_layout);
        smileRating = (SmileRating) findViewById(R.id.smile_rating);
        smileRating.setIndicator(true);

// this works
        smileRating.setSelectedSmile(BaseRating.TERRIBLE);

// this one doesn't works
//      smileRating.setSelectedSmile(BaseRating.TERRIBLE,true);
    }

What am I doing wrong?

Thank you for your AWESOME work! :D

sujithkanna commented 6 years ago

It is because the view is not inflated yet fully, so try posting it using runnable.

smileyRating.post(new Runnable() {
            @Override
            public void run() {
                smileRating.setSelectedSmile(BaseRating.TERRIBLE,true);
            }
        });

Can you try this?

Thank you :D

benoffi7 commented 6 years ago

solution by @sujithkanna works! same problem with animate and the snippet works fine