【问题标题】:Android Widget: RemoteViews with TextView android.content.res.Resources$NotFoundExceptionAndroid 小部件:带有 TextView 的 RemoteViews android.content.res.Resources$NotFoundException
【发布时间】:2015-11-09 07:21:47
【问题描述】:

最近我们将应用中的 targetSDK 从 21 更新到了 23。

android {
  compileSdkVersion 23
  buildToolsVersion '23.0.1'
  ...

defaultConfig {
  applicationId 'com.myapp'
  minSdkVersion 15
  targetSdkVersion 23
  versionCode System.getenv("LAST_BUILD_NUMBER") as Integer ?: 1514261001
  versionName '2.6.1' + (System.getenv("APP_VERSION_SUFFIX") ?: '')
  testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
  multiDexEnabled true
}

我们的应用还有一些小部件,其中包含一些像这样的 TextView:

        <TextView
           android:id="@+id/txt_sunrise"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:drawableLeft="@drawable/ic_classic_weather_sunrise"
           android:drawablePadding="6dp"
           android:text="-"
           android:textColor="@android:color/white"
           android:textSize="@dimen/largeTextSizeAstro" />

小部件是以这种方式构建的:

RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_large);
rv.setTextViewText(R.id.txt_sunrise, sunrise);
// some more code
appWidgetManager.updateAppWidget(appWidgetId, rv);

在 Android 5.0 上一切正常,但在一些 Android 4.x 设备上(我可以在一些三星设备以及 Genymotion 模拟器上重现它),当小部件应该被构建时,我遇到了一个异常:

caused by: android.content.res.Resources$NotFoundException: Resource is not a ColorStateList (color or path): TypedValue{t=0x2/d=0x7f01018e a=2}
at android.content.res.Resources.loadColorStateList(Resources.java:2074)
at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
at android.widget.TextView.<init>(TextView.java:912)
at android.widget.TextView.<init>(TextView.java:578)

当我检查给定的资源-Id 0x7f01018e 时,发现这是应用程序主题中定义的 colorAccent,当然不是颜色状态列表

<style name="AppTheme" parent="Theme.AppCompat">
  <item name="colorPrimary">@color/grey_darker</item>
  <item name="colorPrimaryDark">#111111</item>
  <item name="colorAccent">@color/orange</item>

知道吗,为什么会出现这个错误?

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    我在特定平台和给定的行号上对android Textview的源代码进行了深入研究,我在constructor-method中找到了这个sn-p:

    case com.android.internal.R.styleable.TextView_textColorLink:
         textColorLink = a.getColorStateList(attr);
         break;
    

    我现在的解决方案是将以下行添加到我的主题中,小部件再次工作:

    <item name="android:textColorLink">@color/selector_classic_link</item>
    

    注意:selector_classic_link 是一个在 xml 中定义的 ColorStateList。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      相关资源
      最近更新 更多