Flutter 本机视图:startActivity 无法解析索引 6 处的属性

Flutter native view: startActivity Failed to resolve attribute at index 6

我正在为第 3 方库实现 Flutter 插件,它使用本机视图在 flutter 中显示 Android 视图。该库有一组预定义视图,在单击后启动 Activity - 单击由调用 startActivity.

的 Android 本机代码处理

我遇到的错误:

Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 6: TypedValue{t=0x2/d=0x101009b a=1}
    at android.content.res.TypedArray.getColorStateList(TypedArray.java:596)
    at android.widget.TextView.readTextAppearance(TextView.java:3967)
    at android.widget.TextView.<init>(TextView.java:1027)
    at android.widget.TextView.<init>(TextView.java:968)
    at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:100)
    at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:95)
    at androidx.appcompat.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:194)
    at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:115)
    at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1551)
    at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1602)
    at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1061)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1126)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1263)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1119)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)

在经历了很多类似的问题之后,这个错误最可能的原因是缺少 colorAccent 或其他值的定义。

我设法调试了应用程序并确定索引 6 对应于 com.android.internal.R.styleable.TextAppearance_textColorLink 这是来自 TextView.java:

的完整片段
    private void readTextAppearance(Context context, TypedArray appearance,
        TextAppearanceAttributes attributes, boolean styleArray) {
    final int n = appearance.getIndexCount();
    for (int i = 0; i < n; i++) {
        final int attr = appearance.getIndex(i);
        int index = attr;
        // Translate style array index ids to TextAppearance ids.
        if (styleArray) {
            index = sAppearanceValues.get(attr, -1);
            if (index == -1) {
                // This value is not part of a Text Appearance and should be ignored.
                continue;
            }
        }
        switch (index) {
            case com.android.internal.R.styleable.TextAppearance_textColorHighlight:
                attributes.mTextColorHighlight =
                        appearance.getColor(attr, attributes.mTextColorHighlight);
                break;
            case com.android.internal.R.styleable.TextAppearance_textColor:
                attributes.mTextColor = appearance.getColorStateList(attr);
                break;
            case com.android.internal.R.styleable.TextAppearance_textColorHint:
                attributes.mTextColorHint = appearance.getColorStateList(attr);
                break;
            case com.android.internal.R.styleable.TextAppearance_textColorLink:
          --->      attributes.mTextColorLink = appearance.getColorStateList(attr);
                break;
            case com.android.internal.R.styleable.TextAppearance_textSize:
                attributes.mTextSize =
                        appearance.getDimensionPixelSize(attr, attributes.mTextSize);
                attributes.mTextSizeUnit = appearance.peekValue(attr).getComplexUnit();
                break;

我尝试将以下属性添加到应用程序样式和用于扩展平台视图的样式:

    <style name="MainTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:textAppearance">@style/CustomTextAppearance</item>
    <item name="colorPrimary">#d3b170</item>
    <item name="colorPrimaryDark">#d3b170</item>
    <item name="colorAccent">#d3b170</item>
    <item name="android:textColorPrimary">#d3b170</item>
    <item name="android:textColorLink">#d3b170</item>
    <item name="android:colorAccent">#d3b170</item>
    <item name="android:colorPrimary">#d3b170</item>
    <item name="android:colorPrimaryDark">#d3b170</item>
</style>
<style name="CustomTextAppearance" parent="TextAppearance.AppCompat.Headline">
    <item name="android:textColor">#d3b170</item>
</style>

平台视图本身是通过以下方式创建的:

internal class CustomNativeView(context: Context, id: Int, creationParams: Map<String?, Any?>?) :
PlatformView {
private val view: NativeView

override fun getView(): View {
    return view
}

override fun dispose() {}

init {
    view = NativeView(ContextThemeWrapper(context, R.style.MainTheme), null)
}}

总结一下: CustomNativeView 是在 AndroidView 中显示的平台视图,此视图在单击后(在本机端)启动 activity 由于上述错误而无法扩充。

深入了解库源代码后,我注意到新创建的 Activity 在 onCreate() 中设置了自定义主题。在插件的 style.xml 中覆盖此样式解决了问题:

<style name="Theme.SomePlugin.XYZ.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:textColorLink">#000000</item>
</style>