使用customAttributes扩展customView时,错误扩展类<unknown>

时间:2018-11-18 16:45:23

标签: android custom-attributes android-attributes

我有一个customView

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

如何使错误更具有指示性?

enter image description here

1 个答案:

答案 0 :(得分:0)

我怀疑自定义视图的程序包名称无效。
如果您的软件包名称为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"/>