请帮助:MediaRouteButton无法实例化

时间:2015-02-15 19:13:22

标签: android chromecast google-cast

我对Android编程非常陌生,所以欢迎任何建议。

我正在尝试创建可与Chromecast设备配合使用的应用。我按照示例(CastHelloText),这很好。我也可以向自定义应用发送消息。现在我尝试将转换按钮移动到布局中(而不是在操作栏中)。我的布局文件如下所示:

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

当我进入Android Studio的设计视图时,出现了错误:

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)

这是我的gradle构建文件

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

非常感谢!

1 个答案:

答案 0 :(得分:2)

并非图书馆中的每个自定义View都可以在预览中使用。 MediaRouteButton会有问题。原因是预览实际上是运行该自定义View的代码,但是在Android SDK的非常有限的简化版中,并非所有内容都可用。鉴于错误消息,MediaRouteButton由于某种原因尝试获取ActivityManager,预览不支持。

这不会阻止您的代码工作,但您需要通过原始XML编辑此布局。

  

MediaRouteButton虽然没有显示

这是因为您在布局文件中将其标记为gone