[android] 無法轉換為顏色:在片段中展開佈局時類型= 0x2錯誤但僅在三星Galaxy和註釋4


Answers

我在API 10設備上遇到了同樣的問題,它是由使用API​​ 11+的主題(來自appcompat)引起的。 為了快速解決方案,您可以在styles.xml中找到以下主題聲明

<style name="AppBaseTheme" parent="...">
...
<style name="AppTheme" parent="...">

並改變:

<style name="AppBaseTheme" parent="android:Theme.Light">
...
<style name="AppTheme" parent="android:Theme.Light">
Question

我正在開發一個帶有活動的簡單應用程序,我使用碎片。一些屏幕上有一些元素。 當我編譯並運行應用程序時,它工作正常,除了在三星Galaxy s3和註釋4.我沒有從堆棧跟踪得到它有什麼不對。

01-16 16:25:05.915: E/AndroidRuntime(23174): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.MainActivity}: android.view.InflateException: Binary XML file line #37: Error inflating class com.android.internal.widget.ActionBarView
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.ActivityThread.access$800(ActivityThread.java:163)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.os.Looper.loop(Looper.java:157)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.ActivityThread.main(ActivityThread.java:5335)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at java.lang.reflect.Method.invokeNative(Native Method)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at java.lang.reflect.Method.invoke(Method.java:515)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at dalvik.system.NativeStart.main(Native Method)
01-16 16:25:05.915: E/AndroidRuntime(23174): Caused by: android.view.InflateException: Binary XML file line #37: Error inflating class com.android.internal.widget.ActionBarView
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3253)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3327)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:336)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.Activity.setContentView(Activity.java:1973)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.test.MainActivity.onCreate(MainActivity.java:28)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.Activity.performCreate(Activity.java:5389)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
01-16 16:25:05.915: E/AndroidRuntime(23174):    ... 11 more
01-16 16:25:05.915: E/AndroidRuntime(23174): Caused by: java.lang.reflect.InvocationTargetException
01-16 16:25:05.915: E/AndroidRuntime(23174):    at java.lang.reflect.Constructor.constructNative(Native Method)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.createView(LayoutInflater.java:600)
01-16 16:25:05.915: E/AndroidRuntime(23174):    ... 25 more
01-16 16:25:05.915: E/AndroidRuntime(23174): Caused by: android.view.InflateException: Binary XML file line #35: Error inflating class android.widget.TextView
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.widget.ActionBarView.initTitle(ActionBarView.java:1131)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.widget.ActionBarView.setDisplayOptions(ActionBarView.java:947)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at com.android.internal.widget.ActionBarView.<init>(ActionBarView.java:392)
01-16 16:25:05.915: E/AndroidRuntime(23174):    ... 28 more
01-16 16:25:05.915: E/AndroidRuntime(23174): Caused by: java.lang.reflect.InvocationTargetException
01-16 16:25:05.915: E/AndroidRuntime(23174):    at java.lang.reflect.Constructor.constructNative(Native Method)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.view.LayoutInflater.createView(LayoutInflater.java:600)
01-16 16:25:05.915: E/AndroidRuntime(23174):    ... 38 more
01-16 16:25:05.915: E/AndroidRuntime(23174): Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.content.res.TypedArray.getColor(TypedArray.java:327)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.widget.TextView.<init>(TextView.java:945)
01-16 16:25:05.915: E/AndroidRuntime(23174):    at android.widget.TextView.<init>(TextView.java:863)
01-16 16:25:05.915: E/AndroidRuntime(23174):    ... 41 more

以及片段的佈局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="com.locker.theme.one.MainActivity$PlaceholderFragment" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".14"
        android:clickable="true"
        android:gravity="center" >

        <DigitalClock
            android:id="@+id/digitalClock"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/white"
            android:textSize="66sp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".14"
        android:gravity="center" >

        <ProgressBar
            android:id="@+id/progress"
            style="?android:attr/progressBarStyleInverse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:layout_marginBottom="8dp"
            android:visibility="gone" />

        <Button
            android:id="@+id/activateBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/material_deep_teal_200"
            android:gravity="center"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:text="Test" />
    </RelativeLayout>

</LinearLayout>

這是我使用的風格:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.


    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.


        -->
    </style>

    <!-- Application theme. -->

    <style name="AppTheme" parent="android:Theme.Material">
        <!-- Main theme colors -->
    </style>

</resources>

我做了一點谷歌,但沒有找到可以告訴我發生了什麼的想法。 任何積分都會很棒。 謝謝




我將gradle升級到3.0.1後,在使用第三方庫https://github.com/hoang8f/android-flat-button時遇到了這個問題。

不理想,但降級gradle似乎解決了錯誤

此更改需要更新2個文件:

  1. 項目build.gradle with

    com.android.tools.build:gradle:2.3.3

  2. gradle / wrapper / gradle-wrapper.properties with

    distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip




Related