Art
Art

Reputation: 472

Please help: MediaRouteButton could not be instantiated

I'm very new to Android programming, so any suggestions are welcome.

I'm trying to create an app that would work with a Chromecast device. I followed the example (CastHelloText) and that worked fine. I could send message to a custom app as well. Now I'm trying to move the cast button into the layout (instead of in the action bar). My layout file looks like this:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".JoinActivity">

<android.support.v7.app.MediaRouteButton
    android:id="@+id/media_route_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

When I go into Design view in Android Studio, there's an error:

Rendering Problems
Missing styles. Is the correct theme chosen for this layout?

The following classes could not be instantiated:
    - android.support.v7.app.MediaRouteButton

Exception Details
java.java.lang.UnsupportedOperationException: Unsupported Service: activity
    at com.android.layoutlib.bridge.android.BridgeContext.getSystemService(BridgeContext.java:463)
    at android.support.v7.media.MediaRouter$GlobalMediaRouter.<init>(MediaRouter.java:1528)

Here's my gradle build file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.tosborvorn.art.catancast"
        minSdkVersion 18
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    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:21.0.3'
    compile "com.android.support:mediarouter-v7:21.0.3"
    compile "com.google.android.gms:play-services:6.5.87"
    compile "com.android.support:gridlayout-v7:21.0.0"
}

Thanks so much!

Upvotes: 0

Views: 856

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007534

Not every custom View from a library will work in previews. MediaRouteButton is one that will have issues. The reason is that the previews are really running the code for that custom View, but in a very limited cut-down edition of the Android SDK, and not everything is available. Given the error message, MediaRouteButton is trying to get ActivityManager for some reason, and that is not supported in the preview.

This will not stop your code from working, but you will need to edit this layout via the raw XML.

the MediaRouteButton doesn't show up though

That is because you have it marked as gone in your layout file.

Upvotes: 2

Related Questions