使用v7支持库AlertDialog时的Robolectric InflateException [英] Robolectric InflateException when using v7 support library AlertDialog

查看:54
本文介绍了使用v7支持库AlertDialog时的Robolectric InflateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用普通的android.app.AlertDialogShadowAlertDialog.getLatestAlertDialog()一起使用,但是如果使用支持库android.support.v7.app.AlertDialog,则会发生此异常:

Using a normal android.app.AlertDialog works with ShadowAlertDialog.getLatestAlertDialog(), but if you use a support library android.support.v7.app.AlertDialog, then this exception happens:

android.view.InflateException: XML file app/build/intermediates/res/qa/debug/layout/abc_alert_dialog_material.xml line #-1 (sorry, not yet implemented): Error inflating class android.support.v7.internal.widget.DialogTitle
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
    at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:75)
    at android.support.v7.app.AlertController.installContent(AlertController.java:216)
    at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:240)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:361)
    at android.app.Dialog.show(Dialog.java:262)
    at org.robolectric.shadows.ShadowDialog.show(ShadowDialog.java:65)
    at android.app.Dialog.show(Dialog.java)
<snip>
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -9
    at java.lang.String.substring(String.java:1955)
    at org.robolectric.res.ResName.qualifyResName(ResName.java:51)
    at org.robolectric.res.Attribute.getStyleReference(Attribute.java:147)
    at org.robolectric.res.builder.XmlFileBuilder$XmlResourceParserImpl.getResourceId(XmlFileBuilder.java:789)

这是一个已知问题,但是解决该问题的最佳方法是什么?
https://github.com/robolectric/robolectric/issues/1736

This is a known issue, but what's the best way to workaround it?
https://github.com/robolectric/robolectric/issues/1736

推荐答案

我使用静态实用程序来构建我的AlertDialog对象,当我在类路径上检测到Robolectric时,我将构建一个android.app.AlertDialog对象.这是我的代码:

I use a static utility to build my AlertDialog objects, and I build a android.app.AlertDialog one when I detect Robolectric on the classpath. Here's my code:

private static Boolean isRobolectricTest = null;

/**
 * Determines if we are running inside of Robolectric or not.
 */
public static boolean isARobolectricUnitTest() {
    if (isRobolectricTest == null) {
        try {
            Class.forName("org.robolectric.Robolectric");
            isRobolectricTest = true;
        } catch (ClassNotFoundException e) {
            isRobolectricTest = false;
        }
    }
    return isRobolectricTest;
}

/**
 * This utility helps us to workaround a Robolectric issue that causes our unit tests to fail
 * when an Activity/Fragment creates an AlertDialog using the v7 support library.  The
 * workaround is to use the normal android.app.AlertDialog when running Robolectric tests.
 *
 * The Robolectric bug is: https://github.com/robolectric/robolectric/issues/1736
 *   android.view.InflateException: XML file app/build/intermediates/res/qa/debug/layout/abc_alert_dialog_material.xml line #-1 (sorry, not yet implemented): Error inflating class android.support.v7.internal.widget.DialogTitle
 */
public static DialogInterface createAndShowDialog(Context context,
                                                  @StringRes int titleResId,
                                                  String message,
                                                  @StringRes int negativeTextResId,
                                                  DialogInterface.OnClickListener negativeClickListener,
                                                  @StringRes int neutralTextResId,
                                                  DialogInterface.OnClickListener neutralClickListener,
                                                  @StringRes int positiveTextResId,
                                                  DialogInterface.OnClickListener positiveClickListener,
                                                  boolean cancelable) {
    if (isARobolectricUnitTest()) {
        return UiUtils.createDialog(context, titleResId, message, negativeTextResId, negativeClickListener, neutralTextResId, neutralClickListener, positiveTextResId, positiveClickListener, cancelable);
    } else {
        return UiUtils.createDialogSupportV7(context, titleResId, message, negativeTextResId, negativeClickListener, neutralTextResId, neutralClickListener, positiveTextResId, positiveClickListener, cancelable);
    }
}

private static android.app.AlertDialog createDialog(Context context,
                                                    @StringRes int titleResId,
                                                    String message,
                                                    @StringRes int negativeTextResId,
                                                    DialogInterface.OnClickListener negativeClickListener,
                                                    @StringRes int neutralTextResId,
                                                    DialogInterface.OnClickListener neutralClickListener,
                                                    @StringRes int positiveTextResId,
                                                    DialogInterface.OnClickListener positiveClickListener,
                                                    boolean cancelable) {
    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
    builder.setTitle(titleResId);
    builder.setMessage(message);
    builder.setNegativeButton(negativeTextResId, negativeClickListener);

    if ((neutralTextResId != -1) && (neutralClickListener != null)) {
        builder.setNeutralButton(neutralTextResId, neutralClickListener);
    }

    builder.setPositiveButton(positiveTextResId, positiveClickListener);
    builder.setCancelable(cancelable);

    android.app.AlertDialog alertDialog = builder.create();
    alertDialog.show();

    return alertDialog;
}

private static android.support.v7.app.AlertDialog createDialogSupportV7(Context context,
                                                                        @StringRes int titleResId,
                                                                        String message,
                                                                        @StringRes int negativeTextResId,
                                                                        DialogInterface.OnClickListener negativeClickListener,
                                                                        @StringRes int neutralTextResId,
                                                                        DialogInterface.OnClickListener neutralClickListener,
                                                                        @StringRes int positiveTextResId,
                                                                        DialogInterface.OnClickListener positiveClickListener,
                                                                        boolean cancelable) {
    android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
    builder.setTitle(titleResId);
    builder.setMessage(message);
    builder.setNegativeButton(negativeTextResId, negativeClickListener);

    if ((neutralTextResId != -1) && (neutralClickListener != null)) {
        builder.setNeutralButton(neutralTextResId, neutralClickListener);
    }

    builder.setPositiveButton(positiveTextResId, positiveClickListener);
    builder.setCancelable(cancelable);

    android.support.v7.app.AlertDialog alertDialog = builder.create();
    alertDialog.show();

    return alertDialog;
}

这并不理想,但是可以完成工作,而且在修正了Robolectric问题之后,很容易将黑客删除.

It's not ideal, but it does the job, and it's easy to remove the hack later when the Robolectric issue is fixed.

这篇关于使用v7支持库AlertDialog时的Robolectric InflateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆