简体   繁体   English

我在为图像制作动画时在 android 工作室中收到 null object 参考错误

[英]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 .看来您引用的是drawable id而不是view id Use R.id for views, R.drawable is for drawable resource.R.id用于视图, R.drawable用于可绘制资源。

Change from:更改自:

R.drawable.bart

in to:进入:

R.id.bart

And are you sure that in onCreate() you call setContentView() method (which inflates layout)?您确定在onCreate()中调用setContentView()方法(它会膨胀布局)吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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