Java androidx.mediarouter.app.MediaRouteButton抛出android.view.InflateException,因为背景半透明

Java androidx.mediarouter.app.MediaRouteButton抛出android.view.InflateException,因为背景半透明,java,android,flutter,flutter-plugin,Java,Android,Flutter,Flutter Plugin,在过去的几天里,我一直在开发一个颤振插件。我正试图实现和现有的媒体播放器到一个颤振小部件。然而,由于媒体播放器的SDK在播放器视图中使用MediaRouteButtons,我在尝试充气时得到了一个android.view.InflateException 核心原因是背景不能是半透明的。我曾尝试通过自定义主题或使用带有不透明背景的内置主题来设置主要活动的colorPrimary。这没有效果,错误是持久的 我怀疑《颤栗》在混音中的某个地方添加了自己的主题,这就是问题的原因,但我找不到任何有用的信息

在过去的几天里,我一直在开发一个颤振插件。我正试图实现和现有的媒体播放器到一个颤振小部件。然而,由于媒体播放器的SDK在播放器视图中使用MediaRouteButtons,我在尝试充气时得到了一个
android.view.InflateException

核心原因是背景不能是半透明的。我曾尝试通过自定义主题或使用带有不透明背景的内置主题来设置主要活动的
colorPrimary
。这没有效果,错误是持久的

我怀疑《颤栗》在混音中的某个地方添加了自己的主题,这就是问题的原因,但我找不到任何有用的信息

SDK本身不是问题,因为我只使用
MediaRouteButton
在空视图中测试相同的功能。 以下是我的布局:

<androidx.mediarouter.app.MediaRouteButton
        android:id="@+id/media_route_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
这是我尝试使用的自定义主题

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorPrimary">#FFFFFFFF</item>
    </style>
</resources>

#FFFFFFFF
我使用
setTheme()
在插件清单和应用程序清单中,甚至在生成的
MainActivity.java
活动中设置它


似乎没有什么帮助,所以任何线索都会很好。如果有人知道flatter是如何为插件
活动设置主题的,我们将不胜感激。

我通过从传递到
PlatforView
构造函数的
上下文中设置活动主题,成功地解决了这个问题

以前

FlutterPlayerView(Context context, BinaryMessenger messenger, int id) {
    this.channel = new MethodChannel(messenger, CHANNEL + id);
    this.channel.setMethodCallHandler(this);
    View view = LayoutInflater.from(context).inflate(R.layout.button_view, null);
    this.buttonView = view.findViewById(R.id.media_route_button);
}
之后

还有一种方法可以通过在flatter插件中实现
ActivityAware
接口并覆盖
onAttachedToActivity
onretachedtoactivity
方法来实现

例如:

public class YourFlutterPlugin implements FlutterPlugin, ActivityAware{
    //...

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        Context context = binding.getActivity();
        // Your code for sending the context to the view
        // Example:  platformView.setContext(context);
    }

    @Override
    public void onReattachedToActivity(@NonNull ActivityPluginBinding binding) {
        Context context = binding.getActivity();
        // Your code for sending the context to the view
        // Example:  platformView.setContext(context);
    }

  // ...
}
FlutterPlayerView(Context context, BinaryMessenger messenger, int id) {
    this.channel = new MethodChannel(messenger, CHANNEL + id);
    this.channel.setMethodCallHandler(this);
        
    // android:colorPrimary is opaque in AppTheme
    context.setTheme(R.style.AppTheme); 
    View view = LayoutInflater.from(context).inflate(R.layout.button_view, null);
    this.buttonView = view.findViewById(R.id.media_route_button);
}
public class YourFlutterPlugin implements FlutterPlugin, ActivityAware{
    //...

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        Context context = binding.getActivity();
        // Your code for sending the context to the view
        // Example:  platformView.setContext(context);
    }

    @Override
    public void onReattachedToActivity(@NonNull ActivityPluginBinding binding) {
        Context context = binding.getActivity();
        // Your code for sending the context to the view
        // Example:  platformView.setContext(context);
    }

  // ...
}