2017-03-06 5 views
1

私はRobolectricでアクティビティを構築しようとしていますが、アクティビティのXMLを膨張させることができないため失敗しています。私は以下の問題のあるコンポーネントを含んでいます。私はまだオンラインで満足できる答えを見つけることができませんでした。Androidスタイルを適用するときのRobolectricランタイム例外 - ShadowAssetManager.buildAttribute

カスタムシャドウクラスを作成しようとしましたが、動作しませんでした(https://github.com/robolectric/robolectric/issues/2155#issuecomment-283890626)。

これは、Androidの属性を使用している人なら誰でも実行できる基本的な問題のようですので、ここで何が問題になっているのかは分かりません。以下は

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

問題のある属性のスタイルの下

<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" /> 

を含むXMLは、そのスタックトレースです。それはRobolectricのバグだように、私はまだ感じるものの

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) 

EDIT

以下は、私の問題への解決策でした。

style = "?android:attr/indeterminateProgressStyle"からstyle = "@ android:style/Widget.ProgressBar.Small"に変更すると、問題が修正されます。

Robolectric Issue with ProgressBar

答えて

0

私は例外java.lang.RuntimeException: no value for android:attr/indeterminateProgressStyle in theme with applied stylesを取得する回避策を発見しました。以下に示すコードは、適用されたスタイルのテーマで値が見つからなかったために膨張しない他のビューでも機能します。

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

ステップ2:追加影@RunWithに影をアノテーション値以下に見られるように、下記見た

ステップ1)、org.robolectric.shadows.ShadowResources.ShadowThemeを拡張ShadowThemeクラスを作成

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 { 

} 
関連する問題