【问题标题】:Why does applying a theme to an activity cause the following crash?为什么将主题应用于活动会导致以下崩溃?
【发布时间】:2014-09-20 00:46:42
【问题描述】:

我有一个典型的场景 - 主 (ListFragment) 活动,它会在项目点击时触发详细活动。一切正常。

现在我想根据编程条件制作详细视图(红色/绿色)中的所有文本。我知道这样做的方法是使用主题

所以在 values-v14 中(我的三星 Note 1 设备运行的是 android 4.1), 样式.xml

<style name="ExpensesStyle" parent="@android:style/TextAppearance.Medium">
        <item name="android:textColor">@color/expense_background</item>
    </style>

colors.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <color name="credit_background">#6DA000</color>
    <color name="expense_background">#D30A0A</color>
</resources>

androidManifest.xml

<activity
        android:name="com.app.transactions.TransactionActivity"
        android:label="@string/app_name" 
        android:theme="@style/ExpensesStyle">
    </activity>

android:theme 属性会导致以下崩溃。将样式应用于单个视图 (EditText) 可以正常工作。

W/ResourceType(30089): Too many attribute references, stopped at: 0x01010099
W/ResourceType(30089): Too many attribute references, stopped at: 0x0101009a
W/ResourceType(30089): Too many attribute references, stopped at: 0x0101009b
E/com.app.TranAct(30089): Error in creation Tran Detail Activity
E/com.app.TranAct(30089): android.view.InflateException: Binary XML file line #30: Error inflating class <unknown>
E/com.app.TranAct(30089):   at android.view.LayoutInflater.createView(LayoutInflater.java:619)
E/com.app.TranAct(30089):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693)
E/com.app.TranAct(30089):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
E/com.app.TranAct(30089):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
E/com.app.TranAct(30089):   at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
E/com.app.TranAct(30089):   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
E/com.app.TranAct(30089):   at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
E/com.app.TranAct(30089):   at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3163)
E/com.app.TranAct(30089):   at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3223)
E/com.app.TranAct(30089):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:312)
E/com.app.TranAct(30089):   at android.app.Activity.setContentView(Activity.java:1924)
E/com.app.TranAct(30089):   at com.app.transactions.TransactionActivity.onCreate(TransactionActivity.java:30)

Activity.onCreate

@Override
    protected void onCreate(Bundle instanceState) {
        try {
            super.onCreate(instanceState);
            setContentView(R.layout.activity_simple);

            Intent intent = getIntent();
            long transId  = intent.getLongExtra(EXTRA_TRAN_ID, 0);
            boolean isExpense = intent.getBooleanExtra(EXTRA_TRAN_IS_EXPENSE, true);

            if(instanceState != null)
                return;

            Fragment frag = TransactionDetailFragment.newInstance(isExpense ? new ExpenseFragment(): new CreditFragment(),
                                                                transId, this);
            getSupportFragmentManager().beginTransaction()
                                        .add(R.id.simple_fragment_container, frag)
                                        .commit();
        } catch (Exception e) {
            Log.e(TAG, "Error in creation Tran Detail Activity", e);
        }

    }

layout.activity_simple 是一个带有片段占位符的简单框架布局。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/simple_fragment_container" />

片段布局的第 30 行包含

<TextView
    style="?android:listSeparatorTextViewStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/label_category" />

【问题讨论】:

  • 可以发一下活动代码吗?
  • 可能是您在活动中使用的布局?
  • @crazyPixel 有什么具体方法吗?
  • @blackbelt - 堆栈跟踪看起来不像我的代码。加上删除主题属性...视图正确呈现
  • TransactionActivity.onCreate(TransactionActivity.java:30) ?

标签: android android-styles


【解决方案1】:

style="?android:listSeparatorTextViewStyle" 未在parent="@android:style/TextAppearance.Medium" 中定义

视图使用的所有参数都必须在您的样式或父样式中定义。

编辑:

要回答你真正的问题(如何使这些风格发挥作用),这里有一些方向:

创建您的编辑文本样式:

<style name="MyApp_EditText_style" parent="android:Widget.EditText">

// those below are all the items you CAN change,
// but you should only change those that you really need.

        <item name="android:focusable">___value___</item>
        <item name="android:focusableInTouchMode">___value___</item>
        <item name="android:clickable">___value___</item>
        <item name="android:background">___value___</item>
        <item name="android:textAppearance">___value___</item>
        <item name="android:textColor">___value___</item>
        <item name="android:gravity">___value___</item>
</style>

然后在您的活动或应用程序样式中添加编辑文本

<style name="MyApp.Theme" parent="@android:style/Theme.Holo">
     <item name="android:editTextStyle">@style/MyApp_EditText_style</item>
</style>

请注意,活动或应用程序样式 parent 必须是主要 Android theme 之一,例如 HOLO、HOLO.LIGHT、Theme.Light 或其中之一的后代。

【讨论】:

  • developer.android.com/guide/topics/ui/themes.html 示例 :(。我想做的是让活动上的所有可编辑文本都以红色字体显示。我需要在样式中覆盖什么?我明白了我的标签得到红色字体(如果我删除了我想要的 listSeparator 样式..)+ 我的编辑控件仍然是默认黑色。我想要相反的
  • 以不同的方式解决了它。但是感谢您花时间让我度过崩溃:)
【解决方案2】:

Budius 破案后,这就是我实现目标的方法。

另见

第一步:

值\styles.xml 已经定义了一种称为 AppBaseTheme 的样式,并且从它派生了另一种称为 AppBase 的样式。我发现 AppBaseTheme 源自不同的 android 主题(检查 values-v11 或 values-v14 下的类似文件)。 由于我在这里没有任何特定的版本 - 我创建了几个从 AppBase 派生的样式。

    <style name="ExpensesStyle" parent="AppTheme">
        <item name="android:textColorSecondary">@color/expense_caption_color</item>
    </style>

我在浏览了一些平台主题.xml + 实验后发现: textColorPrimary => 适用于编辑控件。 textColorSecondary => 适用于标签。 所以覆盖现有主题中定义的项目。我决定在这里更改标题/标签的颜色 - 看起来更好。

第二步: 在activity::onCreate中,在setContentView()之前调用setTheme(R.style.ExpensesStyle)

如果你不想要编程控制,你可以在AndroidManifest.xml中声明这个,如问题所示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多