[英]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.