Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InflateException while inflating layout with android.support.v7.app.MediaRouteButton #2991

Closed
mykwillis opened this issue Mar 22, 2017 · 10 comments

Comments

@mykwillis
Copy link

Description

When a layout (xml) file contains an android.support.v7.app.MediaRouteButton, Robolectric will throw an InflateException when trying to inflate the view with the following error message:

android.view.InflateException: XML file build/intermediates/res/merged/debug/layout/activity_main.xml line #-1 (sorry, not yet implemented): XML file build/intermediates/res/merged/debug/layout/activity_main.xml line #-1 (sorry, not yet implemented): Error inflating class android.support.v7.app.MediaRouteButton

Caused by: android.view.InflateException: XML file build/intermediates/res/merged/debug/layout/activity_main.xml line #-1 (sorry, not yet implemented): Error inflating class android.support.v7.app.MediaRouteButton Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at android.view.LayoutInflater.$$robo$$createView(LayoutInflater.java:645) at android.view.LayoutInflater.createView(LayoutInflater.java) at android.view.LayoutInflater.$$robo$$createViewFromTag(LayoutInflater.java:787) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java) at android.view.LayoutInflater.$$robo$$createViewFromTag(LayoutInflater.java:727) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java) at android.view.LayoutInflater.$$robo$$rInflate(LayoutInflater.java:858) at android.view.LayoutInflater.rInflate(LayoutInflater.java) at android.view.LayoutInflater.$$robo$$rInflateChildren(LayoutInflater.java:821) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java) at android.view.LayoutInflater.$$robo$$inflate(LayoutInflater.java:518) at android.view.LayoutInflater.inflate(LayoutInflater.java) at android.view.LayoutInflater.$$robo$$inflate(LayoutInflater.java:426) at android.view.LayoutInflater.inflate(LayoutInflater.java) at android.view.LayoutInflater.$$robo$$inflate(LayoutInflater.java:377) at android.view.LayoutInflater.inflate(LayoutInflater.java) at android.support.v7.app.AppCompatDelegateImplV9.$$robo$$setContentView(AppCompatDelegateImplV9.java:292) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java) at android.support.v7.app.AppCompatActivity.$$robo$$setContentView(AppCompatActivity.java:140) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java) at mykwillis.com.mediaroutebutton.MainActivity.onCreate(MainActivity.java:11) at android.app.Activity.$$robo$$performCreate(Activity.java:6679) at android.app.Activity.performCreate(Activity.java) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:231) at org.robolectric.android.controller.ActivityController$1.run(ActivityController.java:140) at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:362) at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:40) at org.robolectric.android.controller.ActivityController.create(ActivityController.java:137) at org.robolectric.android.controller.ActivityController.create(ActivityController.java:147) at mykwillis.com.mediaroutebutton.MainActivityTest.loadActivity(MainActivityTest.java:14) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:487) at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:209) at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:109) at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:36) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:63) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) Caused by: java.lang.ClassCastException: org.robolectric.res.AttrData cannot be cast to org.robolectric.res.StyleData at org.robolectric.shadows.ShadowAssetManager.resolveStyle(ShadowAssetManager.java:617) at org.robolectric.shadows.ShadowAssetManager.buildTypedValue(ShadowAssetManager.java:742) at org.robolectric.shadows.ShadowAssetManager.attrsToTypedArray(ShadowAssetManager.java:793) at org.robolectric.shadows.ShadowResourcesImpl$ShadowThemeImpl.obtainStyledAttributes(ShadowResourcesImpl.java:177) at android.content.res.ResourcesImpl$ThemeImpl.obtainStyledAttributes(ResourcesImpl.java) at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java:1440) at android.content.Context.obtainStyledAttributes(Context.java:587) at android.support.v7.app.MediaRouterThemeHelper.getThemeColor(MediaRouterThemeHelper.java:169) at android.support.v7.app.MediaRouterThemeHelper.getControllerColor(MediaRouterThemeHelper.java:94) at android.support.v7.app.MediaRouterThemeHelper.createThemedContext(MediaRouterThemeHelper.java:62) at android.support.v7.app.MediaRouteButton.(MediaRouteButton.java) at android.view.LayoutInflater.createView(LayoutInflater.java:645) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) at android.view.LayoutInflater.rInflate(LayoutInflater.java:858) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) at android.view.LayoutInflater.inflate(LayoutInflater.java:518) at android.view.LayoutInflater.inflate(LayoutInflater.java:426) at android.view.LayoutInflater.inflate(LayoutInflater.java:377) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at mykwillis.com.mediaroutebutton.MainActivity.onCreate(MainActivity.java:11) at android.app.Activity.performCreate(Activity.java:6679) at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:231) at org.robolectric.android.controller.ActivityController$1.run(ActivityController.java:140) at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:362) at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:40) at org.robolectric.android.controller.ActivityController.create(ActivityController.java:137) at org.robolectric.android.controller.ActivityController.create(ActivityController.java:147) at mykwillis.com.mediaroutebutton.MainActivityTest.loadActivity(MainActivityTest.java:14) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:487) at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:209) at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:109) at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:36) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:63) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84) ... 1 more

It appears the wrapped Exception is probably closer to the root problem:

Caused by: java.lang.ClassCastException: org.robolectric.res.AttrData cannot be cast to org.robolectric.res.StyleData at
org.robolectric.shadows.ShadowAssetManager.resolveStyle(ShadowAssetManager.java:617) at 
org.robolectric.shadows.ShadowAssetManager.buildTypedValue(ShadowAssetManager.java:742) at 
org.robolectric.shadows.ShadowAssetManager.attrsToTypedArray(ShadowAssetManager.java:793) at 
org.robolectric.shadows.ShadowResourcesImpl$ShadowThemeImpl.obtainStyledAttributes(ShadowResourcesImpl.java:177) at android.content.res.ResourcesImpl$ThemeImpl.obtainStyledAttributes(ResourcesImpl.java) at 
android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java:1440) at android.content.Context.obtainStyledAttributes(Context.java:587) at 
android.support.v7.app.MediaRouterThemeHelper.getThemeColor(MediaRouterThemeHelper.java:169) at 
android.support.v7.app.MediaRouterThemeHelper.getControllerColor(MediaRouterThemeHelper.java:94) at 
android.support.v7.app.MediaRouterThemeHelper.createThemedContext(MediaRouterThemeHelper.java:62) at android.support.v7.app.MediaRouteButton.(MediaRouteButton.java) at 
android.view.LayoutInflater.createView(LayoutInflater.java:645)

Steps to Reproduce

Create an Android Studio project with an empty MainActivity.
Add an <android.support.v7.app.MediaRouteButton> element (fromcom.android.support:mediarouter-v7:25.3.0) to the layout.xml.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="mykwillis.com.mediaroutebutton.MainActivity">

    <android.support.v7.app.MediaRouteButton
        android:id="@+id/video_layer_chromecast_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"/>

</android.support.constraint.ConstraintLayout>

Create a Robolectric test case that attempts to load the MainActivity.

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class MainActivityTest {
    @Test
    public void loadActivity() {
        MainActivity activity = Robolectric.buildActivity(MainActivity.class).create().get();
    }
}

I have a minimal sample project that exhibits the problem on Github here

Robolectric & Android Version

I am using the latest Robolectric (3.3.1) and the most recent Android development tools.

From my project build.gradle:

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "mykwillis.com.mediaroutebutton"
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.3.0'
    compile 'com.android.support:mediarouter-v7:25.3.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    testCompile "org.robolectric:robolectric:3.3.1"
    testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'   // https://github.com/robolectric/robolectric/issues/1932
}

@bartoszwilk
Copy link

+1

1 similar comment
@caltseng
Copy link

+1

@jongerrish
Copy link
Contributor

We've revamped resources in Robolectric 4.0 to process the binary resource files + arsc table produced by aapt and the resource handling code is now much closer to that of the framework. This includes changes to parse the merged manifest using the Android platform code.

Mind giving this a try with 4.0 + binary resources by configuring your gradle.build as follows:-

testImplementation "org.robolectric:robolectric:4.0"

android {
  enableUnitTestBinaryResources=true
  testOptions {
    unitTests {
      includeAndroidResources = true
    }
  }
}

We're doing a big bug scrub and trying to be aggressive about it so apologies if this was closed in error, in which case feel free to reopen.

If this is still an issue an example project with failing test would be most helpful.

@mykwillis
Copy link
Author

Hi @jongerrish trying to verify with my sample app (https://github.com/mykwillis/android-mediaroutebutton-test), but get a gradle error when changing robolectric version to 4.0. Is this a known issue?

dependencies {
    compile 'com.android.support:appcompat-v7:28.0.0'
    compile 'com.android.support:mediarouter-v7:28.0.0'
    testCompile 'junit:junit:4.12'
    //testCompile "org.robolectric:robolectric:3.3.1" // This line works fine
    testCompile "org.robolectric:robolectric:4.0"   // "Module 'org.robolectric:robolectric:4.0' depends on one or more Android Libraries but is a jar"
}

@jongerrish
Copy link
Contributor

jongerrish commented Nov 1, 2018 via email

@xian
Copy link
Member

xian commented Nov 1, 2018

@mykwillis — make sure you're using the latest Android Gradle Plugin.

@mykwillis
Copy link
Author

mykwillis commented Nov 1, 2018

Thanks @xian upgrading the Gradle Plugin made it past that step.
Now I have to figure out why I'm getting:

Could not set unknown property 'enableUnitTestBinaryResources' for object of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.

When I add:

android {
    // ...
    //enableUnitTestBinaryResources=true  // "Could not set unknown property 'enableUnitTestBinaryResources' for object of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
//...
}

to build.gradle. (Using com.android.tools.build:gradle:3.2.1)

https://github.com/mykwillis/android-mediaroutebutton-test/blob/master/app/build.gradle

@jongerrish
Copy link
Contributor

jongerrish commented Nov 1, 2018 via email

@mykwillis
Copy link
Author

ok got it!

Sorry for all the back-and-forth. I can now confirm that the original problem reported in this ticket is fixed with robolectric 4.0 when using the directives you describe above. Example project has been updated at: https://github.com/mykwillis/android-mediaroutebutton-test

@xian
Copy link
Member

xian commented Nov 1, 2018

Huzzah!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants