使用普通的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)
这是一个已知问题,但解决问题的最佳方法是什么?
https://github.com/robolectric/robolectric/issues/1736
最佳答案
我使用静态实用程序来构建我的AlertDialog对象,当我在类路径上检测到Robolectric时,我构建了一个android.app.AlertDialog.这是我的代码:
原文链接:https://www.f2er.com/android/431305.htmlprivate 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问题后很容易删除黑客攻击.