gpt4 book ai didi

android - 使用 v7 支持库 AlertDialog 时 Robolectric InflateException

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:36:35 29 4
gpt4 key购买 nike

使用普通的 android.app.AlertDialogShadowAlertDialog.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)
<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

最佳答案

我使用静态实用程序构建我的 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, 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 问题后很容易删除黑客攻击。

关于android - 使用 v7 支持库 AlertDialog 时 Robolectric InflateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31868325/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com