1 黑马币
一个已经工作的同学,在使用Fresco这个强大的图片处理框架的时候,加载错误
Error inflating class com.facebook.drawee.view.SimpleDraweeView
android.view.InflateException: Binary XML file line #14: Error inflating class com.facebook.drawee.view.SimpleDraweeView
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NullPointerException
at com.facebook.drawee.view.DraweeView.setImageDrawable(DraweeView.java:140)
这里的问题在XML中使用了一个自定义控件com.facebook.drawee.view.SimpleDraweeView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="20dp"
android:layout_height="20dp"
fresco:placeholderImage="@drawable/my_drawable" />
</LinearLayout> 复制代码
异常信息说,在加载xml文件的时候,不能inflating出SimpleDraweeView对象
我们再来看看activity是在哪里加载xml的,SimpleDraweeView又是在哪里初始化的
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fresco.initialize(this);
Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/fresco-logo.png");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);
} 复制代码
可以看到我们是在setContentView(R.layout.activity_main);代码去加载xml对象的
然后再初始化Fresco.initialize(this);对象
所以我们只需要把Fresco.initialize(this);这个初始化后,然后再使用这个框架的自定义控件,就不会报错了
xml文件无需该
activity修改成:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fresco.initialize(this);
setContentView(R.layout.activity_main);
Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/fresco-logo.png");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);
} 复制代码
我来回答