java.lang.IllegalArgumentException:ID 为 2131558727 的菜单项没有 MediaRouteActionProvider

java.lang.IllegalArgumentException: menu item with ID 2131558727 doesn't have a MediaRouteActionProvider

您好,我正在将 chromecast 功能集成到我现有的 app.i 中,代码如下:

public class VideoBrowserActivity extends AppCompatActivity {

    private static final String TAG = "VideoBrowserActivity";
    private boolean mIsHoneyCombOrAbove = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
    private Toolbar mToolbar;
    private CastContext mCastContext;
    private MenuItem mediaRouteMenuItem;
    private IntroductoryOverlay mIntroductoryOverlay;
    private CastStateListener mCastStateListener;

    /*
     * (non-Javadoc)
     * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_browser);
        setupActionBar();

        mCastStateListener = new CastStateListener() {
            @Override
            public void onCastStateChanged(int newState) {
                if (newState != CastState.NO_DEVICES_AVAILABLE) {
                    Log.e("DEBUG","===");
                    showIntroductoryOverlay();
                }
            }
        };

        mCastContext = CastContext.getSharedInstance(this);
    }

    @Override
    protected void onResume() {
        mCastContext.addCastStateListener(mCastStateListener);
        super.onResume();
    }

    @Override
    protected void onPause() {
        mCastContext.removeCastStateListener(mCastStateListener);
        super.onPause();
    }


    private void setupActionBar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle(R.string.app_name);
        setSupportActionBar(mToolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.browse, menu);
        mediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), menu,
                R.id.media_route_menu_item);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent i;
        switch (item.getItemId()) {
            case R.id.action_settings:
                i = new Intent(VideoBrowserActivity.this, CastPreference.class);
                startActivity(i);
                break;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "onDestroy is called");
        super.onDestroy();
    }


    private void showIntroductoryOverlay() {
        if (mIntroductoryOverlay != null) {
            mIntroductoryOverlay.remove();
        }
        if ((mediaRouteMenuItem != null) && mediaRouteMenuItem.isVisible()) {
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    mIntroductoryOverlay = new IntroductoryOverlay.Builder(
                            VideoBrowserActivity.this, mediaRouteMenuItem)
                            .setTitleText("Introducing Cast")
                            .setSingleTime()
                            .setOnOverlayDismissedListener(
                                    new IntroductoryOverlay.OnOverlayDismissedListener() {
                                        @Override
                                        public void onOverlayDismissed() {
                                            mIntroductoryOverlay = null;
                                        }
                                    })
                            .build();
                    mIntroductoryOverlay.show();
                }
            });
        }
    }
}

这些是 xml 个文件

browswer.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
            android:id="@+id/media_route_menu_item"
            android:title="@string/media_route_menu_title"
            app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
            app:showAsAction="always"/>

    <item
            android:id="@+id/action_settings"
            android:title="@string/action_settings"
            app:showAsAction="never"/>

</menu>

和expanded_controller.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
            android:id="@+id/media_route_menu_item"
            android:title="@string/media_route_menu_title"
            app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
            app:showAsAction="always"/>

</menu>

当我运行这段代码时,我收到以下错误

j

ava.lang.IllegalArgumentException: menu item with ID 2131558727 doesn't have a MediaRouteActionProvider.
                                                                               at com.google.android.gms.cast.framework.CastButtonFactory.setUpMediaRouteButton(Unknown Source)
                                                                               at com.calkins.tv_everywhere.VideoBrowserActivity.onCreateOptionsMenu(VideoBrowserActivity.java:1168)
                                                                               at android.app.Activity.onCreatePanelMenu(Activity.java:3128)
                                                                               at com.android.internal.policy.PhoneWindow.preparePanel(PhoneWindow.java:648)
                                                                               at com.android.internal.policy.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:1029)
                                                                               at com.android.internal.policy.PhoneWindow.run(PhoneWindow.java:341)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:158)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7231)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

为什么会出现这个错误我confused.Any求助...

您在代码中 browse.xml 膨胀:

getMenuInflater().inflate(R.menu.browse, menu);

当您将菜单命名为 browser.xml(注意附加的 "r")时,这是一个拼写错误吗?基于此,您可能选错了菜单。

如果您正在构建带有 minifyEnabled true 的发布 apk, 将这些行添加到您的混淆器文件

-keep class android.support.v7.app.MediaRouteActionProvider{
  *;
}
-keep class androidx.mediarouter.app.MediaRouteActionProvider{
*;
}

在文件 menu/main.xml(或包含您的菜单的其他 XML)中替换为:

app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"

由此:

app:actionProviderClass="androidx.mediarouter.app.MediaRouteActionProvider"