2016-08-16 83 views
0

我有2個apk,主要和第二個只有第一個apk使用的資源。我試圖誇大佈局無法從共享資源中充氣CardView

mPackName = mContext.getPackageName() + "remote_package_prefix"; 
mThemeContext = mContext.createPackageContext(mPackName, Context.CONTEXT_IGNORE_SECURITY); 
mResources = mThemeContext.getResources(); 
mInflater = LayoutInflater.from(mThemeContext); 
ViewGroup viewGroup = (ViewGroup) mInflater.inflate(mResources.getLayout(getRemoteId(R.layout.remote_layout)), this, true); 

,並得到這個錯誤

android.view.InflateException: Binary XML file line #20: Error inflating class <unknown> 
Caused by:java.lang.reflect.InvocationTargetException 
Caused by:android.content.res.Resources$NotFoundException: Resource ID #0x7f070001 
at android.content.res.Resources.getValue(Resources.java:1233) 
at android.content.res.Resources.getColor(Resources.java:887) 
at android.support.v7.widget.CardView.initialize(CardView.java:228) 
at android.support.v7.widget.CardView.<init>(CardView.java:112) 

#0x7f070001是CardView

如何解決這個問題?

修訂 CardView XML

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="@dimen/number_item_padding_h" 
    android:layout_marginLeft="@dimen/number_item_padding_v" 
    android:layout_marginRight="@dimen/number_item_padding_v" 
    android:layout_marginTop="@dimen/number_item_padding_h" 
    card_view:cardCornerRadius="4dp" /> 
+0

在這個問題中包含您的佈局文件XML – Booger

+0

我假設它找不到名爲「CardView」的樣式 – Hahaido

+0

您是否包含了gradle依賴項(假設沒有它會編譯)。另外,您是否確保版本與AppCompat版本相匹配(即兩者均爲23.4.1)? – Booger

回答

0

終於有工作。只需將cardBackgroundColor設置爲自定義值即可。我完全不明白爲什麼不使用默認cardBackgroundColor價值的工作...

最後,我不得不定義我的自定義CardView風格,它在我的佈局分配給每個CardView

<style name="AppTheme.CardView" parent="CardView"> 
    <item name="cardBackgroundColor">@android:color/white</item> 
    <item name="cardCornerRadius">4dp</item> 
    <item name="cardElevation">2dp</item> 
    <item name="cardMaxElevation">2dp</item> 
    <item name="cardUseCompatPadding">false</item> 
    <item name="cardPreventCornerOverlap">true</item> 
</style>