0

I want when a user clicks on "ans_three_btn_one" button than a dialog appears and in that dialogbox, I want to implement rorate animation on sunburst imageview. The only problem in this code is "sunburst.startAnimation". If I remove this line then code work properly but no animation. With this line of code my app crashing. Appreciate If you help me.

    public class GamePlay extends AppCompatActivity implements View.OnClickListener    {

    Button ans_three_btn_one;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_play);

        ans_three_btn_one = findViewById(R.id.ans_three_first_btn);
        ans_three_btn_one.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Dialog first_prize_dialog = new Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        first_prize_dialog.setContentView(R.layout.activity_first_prize);
        first_prize_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        first_prize_dialog.show();

        ImageView sunbrust = findViewById(R.id.sunbrust_img);
        //----Error area start ---
        sunbrust.startAnimation(AnimationUtils.loadAnimation(v.getContext(),R.anim.rotate));
        //----Error area end ---
    }




java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.startAnimation(android.view.animation.Animation)' on a null object reference

Application terminated.
Qazi Hassan
  • 35
  • 1
  • 7
  • 1
    Apparently, `sunbrust` is `null` when you call `startAnimation`. Are you sure you are applying the correct `R.id...` from the xml source? Have you tried moving the definition to the `onCreate` method? – deHaar Aug 14 '18 at 12:06

1 Answers1

3

You have missed first_prize_dialog before findViewById.

It will be

ImageView sunbrust = first_prize_dialog.findViewById(R.id.sunbrust_img);
Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53