starting an app with splash screen but when I run it

Viewed 71

I get this error**"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setAnimation(android.view.animation.Animation)' on a null object reference"****

public class MainActivity extends AppCompatActivity {
   
 private static int SPLASH_SCREEN = 5000;
    Animation topAnim,bottomAnim;
    ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    topAnim = AnimationUtils.loadAnimation(this, R.anim.top_animation);
    bottomAnim = AnimationUtils.loadAnimation(this, R.anim.bottom_animation);


    ImageView Logo = findViewById(R.id.Logo);
    TextView slogan = findViewById(R.id.slogan);

    image.setAnimation(topAnim);
    slogan.setAnimation(bottomAnim);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(MainActivity.this,Dashboard.class);
            startActivity(intent);
            finish();
        }
    },SPLASH_SCREEN);
}

}

3 Answers

Variable image didn't initialize!

You didn't matching image with your xml

image = findViewById(R.id.Logo);

instead of

ImageView Logo = findViewById(R.id.Logo);

You did't initialize image

You have to initialize image before setAnimation to image

like image = findViewById(R.id.your_responsable_image_id);

when you not initialize a view and want to access it , Then you will got NullPointerException

Related