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

时间:2017-04-19 09:49:33

标签: android chromecast

您好我正在将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>

当我运行此代码时,我收到了以下错误

Ĵ

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$1.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)

为什么会出现这个错误我很困惑。任何帮助...

3 个答案:

答案 0 :(得分:4)

如果您要使用minifyEnabled为true构建发行版APK, 将这些行添加到您的proguard文件中

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

答案 1 :(得分:2)

在文件menu / main.xml(或包含菜单的其他XML)上替换以下内容:

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

通过这个:

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

答案 2 :(得分:1)

您在代码中夸大了browse.xml

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

当你拨打你的菜单browser.xml(注意额外的“r”)时,这是一个错字吗?基于此,你可能会选错了菜单。