android 使用< unknown>customAttributes膨胀customView时发生膨胀类错误

n9vozmp4  于 2022-11-20  发布在  Android
关注(0)|答案(2)|浏览(164)

我有自定义视图

<com.google.android.libraries.onegoogle.account.disc.AccountParticleDisc
    android:id="@+id/account_avatar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="?attr/disc_padding"
    android:contentDescription="@null"
    app:imageViewSize="?attr/disc_imageViewSize"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:src="@drawable/quantum_ic_account_circle_googblue_24"/>

和其java:

public AccountParticleDisc(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater.from(context).inflate(R.layout.account_particle_disc, this, true);
    this.imageView = findViewById(R.id.og_apd_internal_image_view);
    badgeWrapper = findViewById(R.id.badge_wrapper);

    TypedArray style = context.obtainStyledAttributes(attrs, R.styleable.AccountParticleDisc);
    int discSize;
    int imageViewSize;
    try {
      avatarSize =
          style.getDimensionPixelSize(R.styleable.AccountParticleDisc_avatarSize, SIZE_NOT_SET);
      discSize =
          style.getDimensionPixelSize(R.styleable.AccountParticleDisc_discSize, SIZE_NOT_SET);
      imageViewSize =
          style.getDimensionPixelSize(R.styleable.AccountParticleDisc_imageViewSize, SIZE_NOT_SET);
    } finally {
      style.recycle();
    }

和这些自定义属性:

<resources>
  <declare-styleable name="AccountParticle">
    <attr name="disc_padding" format="reference"/>
    <attr name="disc_imageViewSize" format="reference"/>
  </declare-styleable>
</resources>

<style name="Theme.ap.header" parent="Theme.AppCompat">
    <item name="text_marginStart">@dimen/account_menu_header_signed_in_avatar_margin_start</item>
    <item name="text_marginEnd">  @dimen/account_menu_header_signed_in_margin_end</item>
    <item name="text_marginLeft"> @dimen/account_menu_header_signed_in_avatar_margin_start</item>
    <item name="text_marginRight">@dimen/account_menu_header_signed_in_margin_end</item>
    <item name="disc_imageViewSize">    @dimen/account_menu_header_signed_in_disc_size</item>
    <item name="disc_padding">    @dimen/account_menu_header_signed_in_avatar_margin_end</item>
  </style>

我膨胀它的viewGroup:

ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(context,
        R.style.Theme_ap_header);
    LayoutInflater.from(contextThemeWrapper).inflate(R.layout.account_particle, this, true);

我得到这个错误:

cause = {InflateException@12215} "android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>"
detailMessage = "Binary XML file line #7: Binary XML file line #7: Error inflating class <unknown>"

我怎样才能使这个错误更具指示性呢?

jaql4c8m

jaql4c8m1#

我怀疑自定义视图的包名称无效。
如果您的套件名称是com.example.app,且您的CustomView位于相同的套件中,则您应该使用-

<com.example.app.CustomView
    android:id="@+id/account_avatar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="?attr/disc_padding"
    android:contentDescription="@null"
    app:imageViewSize="?attr/disc_imageViewSize"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:src="@drawable/quantum_ic_account_circle_googblue_24"/>
up9lanfz

up9lanfz2#

当我的活动是扩展FragmentActivity时,这个问题一直困扰着我。所以有了AppCompatActivity,问题就没有了。

相关问题