3

I am trying to test a fragment, following these instructions: https://developer.android.com/training/basics/fragments/testing

However I am getting the following crash when calling launchFragmentInContainer from my test.

Stacktrace:

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

Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
...
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createView(LayoutInflater.java:647)
...
at com.myapp.poll.PollHomeFragment.onCreateView(PollHomeFragment.kt:31)
...
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f0301b1 a=-1}
at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:946)
at android.content.res.TypedArray.getDrawable(TypedArray.java:930)
at android.view.View.__constructor__(View.java:5010)
at android.view.View.<init>(View.java)
at android.widget.TextView.<init>(TextView.java)
...
at com.myapp.poll.PollHomeFragment.onCreateView(PollHomeFragment.kt:31)

Here's the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".poll.PollActivity">

<TextView
    android:id="@+id/tvEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:text="asdf@asdf.com" />

<TextView                      <!-- This is line 16 -->
    android:id="@+id/tvViewPoll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground"
    android:gravity="center"
    android:minHeight="48dp"
    android:text="View Poll" />

<TextView
    android:id="@+id/tvCreatePoll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground"
    android:gravity="center"
    android:minHeight="48dp"
    android:text="Create Poll" />

tenprint
  • 1,123
  • 1
  • 11
  • 29

3 Answers3

5

OK, from an educated guess based on the stacktrace, I tried removing android:background="?attr/selectableItemBackground" and this resolves the issue.

It appears that ?attr/selectableItemBackground is perhaps not compatible with FragmentScenario or this is a framework bug.

I submitted a bug on the issue tracker: https://issuetracker.google.com/issues/144629519

Update from Google:

Status: Won't Fix (Intended Behavior) The default theme does not have an app defined attribute ?attr/selectableItemBackground (as Fragment has no dependency on AppCompat, MDC, etc)

It sounds like you should be passing the correct theme to FragmentScenario:

launchFragmentInContainer<PollHomeFragment>(themeResId = R.style.Theme_YourTheme)
tenprint
  • 1,123
  • 1
  • 11
  • 29
2

In Java one also has to pass the theme to the FragmentScenario. Else the Fragment will not know about the current theme and will eventually complain that one should set an AppCompat theme ...

FragmentScenario<SomeFragment> scenario = FragmentScenario.launchInContainer(
    SomeFragment.class,
    Bundle.EMPTY,
    R.style.Theme_Custom,
    null
);
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
0

For me, using "android:?attr/selectableItemBackground" instead, solved the problem

Hanoch Moreno
  • 593
  • 7
  • 9
  • 1
    Are you only confirming the answer by JP Lipata oder trying to make an additional contribution? You will have to explain - or delete this post please. – Yunnosch Jul 29 '21 at 08:41