app has stopped (runtime error) onclick

Where do you initialize currentIndex? You’re using currentIndex++; but you never initialize it unless it is your position.

And you’re using return inside an if when you’re using a void from onClick.
If you want to do something like this try something like this:

And change first parameter of Toast to v.getContext()

public void onClick(View v) {
    if (currentIndex == 22) {
        Toast.makeText(v.getContext(), "YOUR MESSAGE", Toast.LENGTH_SHORT).show();
    } else {
        //Code if is not equals to 22
    }
}

Leave a Comment