android – 使用v7支持库AlertDialog时的Robolectric InflateException

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

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)
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

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

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,cancelable);
    }
}

private static android.app.AlertDialog createDialog(Context context,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,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,positiveClickListener);
    builder.setCancelable(cancelable);

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

    return alertDialog;
}

它并不理想,但是它可以完成工作,并且在修复Robolectric问题后很容易删除黑客攻击.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...