使用espresso的Android ui测试无法使用壁画SimpleDreweeView

时间:2017-12-01 09:27:43

标签: android unit-testing android-espresso fresco

现在我想用espresso来测试我的应用程序ui,但是当我使用fresco库,作为xml中的一个SimpleDreweeView时,espresso将失败并出现错误:

android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.facebook.drawee.view.SimpleDraweeView
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474)
at android.app.Activity.setContentView(Activity.java:2388)
at com.example.android.testing.espresso.intents.AdvancedSample.ImageViewerActivity.onCreate(ImageViewerActivity.java:45)

所以任何人都可以帮我解决问题?非常感谢!!

我的xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:padding="@dimen/activity_horizontal_margin"
              tools:context=".ImageViewerActivity">

    <Button android:id="@+id/button_take_photo"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_open_camera"
            android:layout_gravity="center_horizontal"
            android:onClick="onOpenCamera"
            android:layout_marginTop="32dp"/>

    <com.facebook.drawee.view.SimpleDraweeView
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:id="@+id/imageView"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="32dp"
        android:background="#ffdbdbdb"/>

</LinearLayout>

ImageViewActivity:

public class ImageViewerActivity extends Activity {

    @VisibleForTesting
    protected static final String KEY_IMAGE_DATA = "data";

    private static final int REQUEST_IMAGE_CAPTURE = 1;

    private SimpleDraweeView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_viewer);
        mImageView = (SimpleDraweeView) findViewById(R.id.imageView);
    }

    private void dispatchTakePictureIntent() {
        // Open the camera to take a photo.
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    public void onOpenCamera(View view) {
        dispatchTakePictureIntent();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // If an image is received, display it on the ImageView.
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            if (extras == null || !extras.containsKey(KEY_IMAGE_DATA)) {
                return;
            }
            Bitmap imageBitmap = (Bitmap) extras.get(KEY_IMAGE_DATA);
            mImageView.setImageBitmap(imageBitmap);
        }
    }
}

我只是修改谷歌官方演示,将imageview替换为simpledraweeview,并且容易出错....

1 个答案:

答案 0 :(得分:1)

在你写的案例中

@Override
public void onCreate() {
    super.onCreate();
    Fresco.initialize(this);
    setContentView(R.layout.activity_image_viewer);
}