тест не пройден, когда макет содержит <merge>для моего customView - PullRequest
0 голосов
/ 24 ноября 2018

У меня есть этот @before unit-test

  @Before
  public void initialize() {
    View view =
        LayoutInflater.from(RuntimeEnvironment.application)
            .inflate(R.layout.account_list_item, null);
    view.<AccountParticleDisc<Object>>findViewById(R.id.account_avatar)
        .initialize(imageLoader, Object.class);
    Mockito.reset(imageLoader); // Reset since initialize calls load the default image into the disc
    displayName = view.findViewById(R.id.account_display_name);
    accountName = view.findViewById(R.id.account_name);
    accountViewSetter = new AccountViewSetter<>(view, accountConverter);
  }

с этим account_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

  <com.myView1
      android:id="@+id/account_avatar"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="?attr/disc_margin_start"
      android:layout_marginLeft="?attr/disc_margin_start"
      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"/>
  <android.support.constraint.Guideline
      android:id="@+id/account_display_name_top_guideline"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      app:layout_constraintGuide_begin="?attr/account_particle_guideline_vertical_offset"/>
  <TextView
      android:id="@+id/account_display_name"
      style="@style/AccountDataDisplayName"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginStart="?attr/text_marginStart"
      android:layout_marginEnd="?attr/text_marginEnd"
      android:layout_marginLeft="?attr/text_marginStart"
      android:layout_marginRight="?attr/text_marginEnd"
      android:gravity="center_vertical"
      android:includeFontPadding="false"
      app:layout_constraintBottom_toTopOf="@+id/account_name"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0"
      app:layout_constraintStart_toEndOf="@+id/account_avatar"
      app:layout_constraintTop_toBottomOf="@id/account_display_name_top_guideline"
      app:layout_constraintVertical_chainStyle="packed"
      app:layout_constraintWidth_default="wrap"
      tools:text="DisplayNameIssLongSoItShouldBeTruncatedAtSomePoint"/>
  <TextView
      android:id="@+id/account_name"
      style="@style/AccountDataAccountName"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginStart="?attr/text_marginStart"
      android:layout_marginEnd="?attr/text_marginEnd"
      android:layout_marginLeft="?attr/text_marginStart"
      android:layout_marginRight="?attr/text_marginEnd"
      android:gravity="center_vertical"
      android:includeFontPadding="false"
      app:layout_constraintBottom_toTopOf="@+id/account_name_bottom_guideline"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0"
      app:layout_constraintStart_toEndOf="@+id/account_avatar"
      app:layout_constraintTop_toBottomOf="@+id/account_display_name"
      app:layout_constraintWidth_default="wrap"
      tools:text="emailisverylongaswellwewantittogettruncated@gmail.longdomain.com"/>

  <android.support.constraint.Guideline
      android:id="@+id/account_name_bottom_guideline"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      app:layout_constraintGuide_end="?attr/account_particle_guideline_vertical_offset"/>


</android.support.constraint.ConstraintLayout>

, но когда я реорганизую его в этот account_list_it.xml макет.

<?xml version="1.0" encoding="utf-8"?>
<com.MyView2
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/has_selected_account"
    android:layout_width="match_parent"
    android:layout_height="@dimen/account_particle_account_list_item_height"
    app:apStyle="list_item"
    app:layout_constraintHorizontal_bias="0"/>

, который включает в себя все содержимое предыдущего account_list_it.xml макета

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

..

</merge>

, но корневой макет, который является родителем соответствующего пользовательского представления:

publicкласс MyView2 расширяет ConstraintLayout {

Я получаю ошибку:

Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>

android.view.InflateException: Binary XML file line #7: Failed to resolve attribute at index 3: TypedValue{t=0x2/d=0x7f010073 a=-1}

и

android.view.InflateException: 
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>

Я получаю ошибку инфляции.как это может быть?

1 Ответ

0 голосов
/ 24 ноября 2018

Вы должны предоставлять родительское представление всякий раз, когда вы надуваете представление с помощью тега merge:

Context context = RuntimeEnvironment.application;
View parent = new FrameLayout(context);
View view = LayoutInflater.from(context).inflate(R.layout.account_list_item, parent, true);
...