简体   繁体   中英

i am getting a null object reference error in android studio while animating a image

public class MainActivity extends AppCompatActivity {

    public void click(View view) {
        ImageView bart = (ImageView) findViewById(R.drawable.bart);
        bart.animate().alpha(0);
    }
}

This is the error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewPropertyAnimator android.widget.ImageView.animate()' on a null object reference

It seems that you are referencing to a drawable id and not a view id . Use R.id for views, R.drawable is for drawable resource.

Change from:

R.drawable.bart

in to:

R.id.bart

And are you sure that in onCreate() you call setContentView() method (which inflates layout)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM