【问题标题】:Robolectric RuntimeException when applying Android style - ShadowAssetManager.buildAttribute应用 Android 样式时出现 Robolectric RuntimeException - ShadowAssetManager.buildAttribute
【发布时间】:2017-07-26 23:18:49
【问题描述】:

我正在尝试在 Robolectric 中构建一个 Activity,但由于未能为该 Activity 的 XML 充气而失败。我在下面包含了有问题的组件。我还没有在网上找到满意的答案。

我尝试过创建自定义 Shadow 类,但没有成功 (https://github.com/robolectric/robolectric/issues/2155#issuecomment-283890626)。

这似乎是任何使用 Android 属性的人都会遇到的基本问题,所以我不太确定这里出了什么问题。

    mActivity = Robolectric.buildActivity(TestActivity.class)
            .withIntent(intent)
            .create()
            .get();

以下是包含违规属性样式的 XML

<ProgressBar
    android:id="@+id/blocking_progress_bar"
    style="?android:attr/indeterminateProgressStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:indeterminate="true"
    android:interpolator="@android:interpolator/cycle" />

下面是堆栈跟踪。

Caused by: java.lang.RuntimeException: no value for android:attr/indeterminateProgressStyle in theme with applied styles: [Style android:Theme_DeviceDefault (and parents) (forced), Style com.package.name:Theme_XXX (and parents) (forced)]
    at org.robolectric.shadows.ShadowAssetManager.buildAttribute(ShadowAssetManager.java:463)
    at org.robolectric.shadows.ShadowAssetManager.attrsToTypedArray(ShadowAssetManager.java:532)
    at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:213)
    at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java)
    at android.content.Context.obtainStyledAttributes(Context.java:532)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)

编辑

以下是我的问题的解决方案,尽管我仍然觉得这是一个 Robolectric 错误。

从 style="?android:attr/indeterminateProgressStyle" 更改为 style="@android:style/Widget.ProgressBar.Small" 解决了这个问题。

Robolectric Issue with ProgressBar

【问题讨论】:

    标签: android android-styles robolectric android-progressbar


    【解决方案1】:

    我找到了获取异常java.lang.RuntimeException: no value for android:attr/indeterminateProgressStyle in theme with applied styles 的解决方法。下面显示的代码也适用于其他无法膨胀的视图,因为在应用样式的主题中找不到值。

    第 1 步)创建一个扩展 org.robolectric.shadows.ShadowResources.ShadowThemeShadowTheme 类,如下所示:

    import android.content.res.Resources;
    import android.content.res.TypedArray;
    import android.util.AttributeSet;
    import org.robolectric.Robolectric;
    import org.robolectric.annotation.Implementation;
    import org.robolectric.annotation.Implements;
    import org.robolectric.shadows.ShadowResources;
    
    @Implements(value = Resources.Theme.class)
    public class ShadowTheme extends ShadowResources.ShadowTheme {
    
    
    @Implementation
    @Override
    public TypedArray obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes) {
        try {
            return super.obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes);
        } catch (Throwable err) {
            AttributeSet attributeSet = Robolectric.buildAttributeSet()
                    .addAttribute(android.R.attr.layout_width, "0dp")
                    .addAttribute(android.R.attr.layout_height, "0dp")
                    .build();
            return super.obtainStyledAttributes(attributeSet, attrs, defStyleAttr, defStyleRes);
        }
    }
    }
    

    第二步:给@RunWithshadows注解值添加阴影,如下图:

    import android.os.Build;
    import com.superbalist.android.BuildConfig;
    import com.superbalist.android.test.shadow.ShadowTheme;
    import org.junit.runner.RunWith;
    import org.robolectric.RobolectricTestRunner;
    import org.robolectric.annotation.Config;
    
    @RunWith(RobolectricTestRunner.class)
    @Config(constants = BuildConfig.class,
        sdk = Build.VERSION_CODES.N_MR1,
        shadows = {ShadowTheme.class})
    public class MyUnitTest {
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 2015-04-17
      相关资源
      最近更新 更多