androidx.mediarouter.app.MediaRouteButton throwing android.view.InflateException because of translucent background

huangapple go评论93阅读模式
英文:

androidx.mediarouter.app.MediaRouteButton throwing android.view.InflateException because of translucent background

问题

以下是您要求的翻译内容:

我在过去的几天里一直在开发一个 Flutter 插件。我试图将一个现有的媒体播放器实现到一个 Flutter 小部件中。然而,由于媒体播放器的 SDK 在播放器视图中使用了 MediaRouteButtons,所以当我尝试膨胀它时,我遇到了 android.view.InflateException 错误。

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

我怀疑 Flutter 在某个地方添加了自己的主题,这导致了问题,但我找不到任何有用的信息。

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

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

我正在创建一个 PlatformView,在构造函数中我试图膨胀视图。以下是构造函数,而引发异常的是 LayoutInflater

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);
    }

以下是我尝试使用的自定义主题:

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

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

似乎没有任何帮助,所以任何线索都将是极好的。如果有人了解 Flutter 如何为插件 Activity 设置主题,那么对此的解释将会非常赞赏。

英文:

I have been working on a flutter plugin for the last few days. I am trying to implement and existing media player into a flutter widget. However since the media player's SDK uses MediaRouteButtons in the player view I have been getting an android.view.InflateException when trying to inflate it.

The core reason is because the background cannot be translucent. I have tried to set the main activity's colorPrimary through custom themes or using built in themes with opaque backgrounds. This has no effect and the error is persistant.

I suspect that flutter is adding its own theme somewhere in the mix and this is causing the issue, but I couldn't find any helpful information on that.

The SDK itself is not the problem since I am testing the same functionality with an empty view with only a MediaRouteButton.
Here is my layout:

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

I am creating a PlatformView and in the constructor I am trying to inflate the view. This is the constructor and the LayoutInflater is the one throwing the exception.

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);
    }

This is a custom theme I have tried using

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

I set it in the plugin manifest and also in the app manifest, and even in the generated MainActivity.java activity by using setTheme().

Nothing seems to help, so any leads would be great. If someone understands how flutter sets the themes for the plugin Activity an explanation would be greatly appreciated.

答案1

得分: 0

我通过从传递到我的 PlatforView 构造函数中的 context 设置活动的主题来解决了这个问题。

Before

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);
}

After

FlutterPlayerView(Context context, BinaryMessenger messenger, int id) {
    this.channel = new MethodChannel(messenger, CHANNEL + id);
    this.channel.setMethodCallHandler(this);
        
    // android:colorPrimary 在 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);
}

此外,还可以通过在 Flutter 插件中实现 ActivityAware 接口,并覆盖 onAttachedToActivityonReattachedToActivity 方法来实现。

例如:

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

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        Context context = binding.getActivity();
        // 将上下文发送到视图的代码
        // 示例: platformView.setContext(context);
    }

    @Override
    public void onReattachedToActivity(@NonNull ActivityPluginBinding binding) {
        Context context = binding.getActivity();
        // 将上下文发送到视图的代码
        // 示例: platformView.setContext(context);
    }

  // ...
}
英文:

I managed to resolve the issue by setting the theme of the activity from the context passed into my PlatforView constructor

Before

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);
    }

After

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);
}

Also there is a way to do it by implementing the ActivityAware interface in your Flutter plugin and overriding the onAttachedToActivity and onRettachedToActivity methods.

For example:

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);
    }

  // ...
}

</details>



huangapple
  • 本文由 发表于 2020年10月4日 00:47:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64186672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定