android - 为什么将主题应用于 Activity 会导致以下崩溃?

标签 android android-styles

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

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

因此在 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>

颜色.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 属性导致以下崩溃。将样式应用于单个 View (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 是一个带有 fragment 占位符的简单框架布局。

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

fragment 布局的第30行包含

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

最佳答案

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

中定义

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

编辑:

要回答您真正的问题(如何使这些样式起作用),请引用以下方向:

创建您的编辑文本样式:

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

然后在您的 Activity 或 Application 样式上添加编辑文本

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

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

关于android - 为什么将主题应用于 Activity 会导致以下崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25935793/

相关文章:

android - 在android中停止服务

javascript - 有没有办法从移动浏览器调用导航?

java - 将 jar 导入 android 项目时出错

android - stroke 属性不适用于 android studio 中的按钮

android - 使图像的透明部分不可点击

java - 自定义editText字段的密码转换方法

android - 如何更改 EditText 句柄的颜色?

android - Edittext样式不打开键盘

android - 如何仅在 react 原生的底部设置高程阴影?

java - 对话框 fragment 中未显示标题