尝试调用虚方法 'void android.support.v7.app.MediaRouteActionProvider.setRouteSelector

Attempt to invoke virtual method 'void android.support.v7.app.MediaRouteActionProvider.setRouteSelector

我正在

Attempt to invoke virtual method 'void android.support.v7.app.MediaRouteActionProvider.setRouteSelector(android.support.v7.media.MediaRouteSelector)' on a null object reference

当我想为我的应用添加演员表支持时,我已经在一个单独的项目中创建了所有内容,现在我只想将所有内容转移到我的应用中,但我得到了这个 exception.I 看不到哪里有我犯了错误。 这是我的代码:

 ......

    private MediaRouter mMediaRouter;
    private MediaRouteSelector mMediaRouteSelector;
    private MediaRouter.Callback mMediaRouterCallback;
    private CastDevice mSelectedDevice;
    private GoogleApiClient mApiClient;
    private Cast.Listener mCastListener;
    private GoogleApiClient.ConnectionCallbacks mConnectionCallbacks;
    private ConnectionFailedListener mConnectionFailedListener;
    private HelloWorldChannel mHelloWorldChannel;
    private boolean mApplicationStarted;
    private boolean mWaitingForReconnect;
    private String mSessionId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //for transaltion

        SharedPreferences LengPref = getSharedPreferences("language",
                MODE_PRIVATE);
        String langStr = LengPref.getString("lang", "");

        String languageToLoad = langStr;
        Locale locale = new Locale(languageToLoad);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, null);
        // for transaltion
        super.onCreate(savedInstanceState);




        // Configure Cast device discovery
        mMediaRouter = MediaRouter.getInstance(getApplicationContext());
        mMediaRouteSelector = new MediaRouteSelector.Builder()
                .addControlCategory(CastMediaControlIntent.categoryForCast(getResources()
                        .getString(R.string.app_id))).build();
        mMediaRouterCallback = new MyMediaRouterCallback();




       .....
    }




    private class MyMediaRouterCallback extends MediaRouter.Callback {

        @Override
        public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo info) {
            //Log.d(TAG, "onRouteSelected");
            // Handle the user route selection.
            mSelectedDevice = CastDevice.getFromBundle(info.getExtras());

            launchReceiver();
        }

        @Override
        public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo info) {
            //Log.d(TAG, "onRouteUnselected: info=" + info);
            teardown(false);
            mSelectedDevice = null;
        }
    }


   .......

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();

        //getMenuInflater().inflate(R.menu.main, menu);


        if (workoutTypeFinal == 0) {
            inflater.inflate(R.menu.workout_menu, menu);


        } else if (workoutTypeFinal == 2 || workoutTypeFinal == 3) {
            inflater.inflate(R.menu.workout_menu, menu);
        } else {
            //ovo je ako je next next tip
            inflater.inflate(R.menu.workout_menu_2, menu);
        }

        MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
        MediaRouteActionProvider mediaRouteActionProvider
                = (MediaRouteActionProvider) MenuItemCompat
                .getActionProvider(mediaRouteMenuItem);
        // Set the MediaRouteActionProvider selector for device discovery.
        mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector);

        mOptionsMenu = menu;
        return super.onCreateOptionsMenu(menu);
    }

    .....
    @Override
    protected void onStop() {
        mMediaRouter.removeCallback(mMediaRouterCallback);
        super.onStop();

        if (hasTimer == true) {
            timer5.cancel();
            timer2.cancel();
        }

        if (workoutTypeFinal == 0) {
            //inflater.inflate(R.menu.workout_menu, menu);
            if (isTimer1 == true) {
                timer1.cancel();
                //timer5.cancel();
                //mp.stop();
            } else if (isTimer2 == true) {
                timer2.cancel();
                timer5.cancel();
                if (isMusic == true) {
                    mp.stop();
                } else {

                }
            } else if (isTimer3 == true) {
                timer3.cancel();
                timer5.cancel();
                mp.stop();
            } else if (isTimer4 == true) {
                timer4.cancel();
                timer5.cancel();
                mp.stop();
            }
        } else if (workoutTypeFinal == 2) {
            //inflater.inflate(R.menu.workout_menu, menu);


        } else {
            //ovo je ako je next next tip
            //inflater.inflate(R.menu.workout_menu_2, menu);
        }


    }

    @Override
    protected void onPause() {
        super.onPause();
        //pausedVoid=false;


        if (workoutTypeFinal == 0) {
            if (pausedVoid == true) {

            } else {
                pauseTime();
            }

        } else if (workoutTypeFinal == 2) {
            if (pausedVoid == true) {

            } else {
                pauseTime();
            }

            //asd

        } else {
            //ovo je ako je next next tip

        }


    }

    @Override
    protected void onDestroy() {
        teardown(true);
        super.onDestroy();
        if (hasTimer == true) {
            timer5.cancel();
            timer2.cancel();
        }

        if (workoutTypeFinal == 0) {
            //inflater.inflate(R.menu.workout_menu, menu);
            if (isTimer1 == true) {
                timer1.cancel();
                //timer5.cancel();
                //mp.stop();
            } else if (isTimer2 == true) {
                timer2.cancel();
                timer5.cancel();
                if (isMusic == true) {
                    mp.stop();
                } else {

                }
            } else if (isTimer3 == true) {
                timer3.cancel();
                timer5.cancel();
                mp.stop();
            } else if (isTimer4 == true) {
                timer4.cancel();
                timer5.cancel();
                mp.stop();
            }
        } else if (workoutTypeFinal == 2) {
            //inflater.inflate(R.menu.workout_menu, menu);
            if (isTimer1 == true) {
                timer1.cancel();
                //timer5.cancel();
                //mp.stop();
            } else if (isTimer2 == true) {
                timer2.cancel();
                timer5.cancel();
                if (isMusic == true) {
                    mp.stop();
                } else {

                }
            } else if (isTimer3 == true) {
                timer3.cancel();
                timer5.cancel();
                mp.stop();
            } else if (isTimer4 == true) {
                timer4.cancel();
                timer5.cancel();
                mp.stop();
            }

        } else {
            //ovo je ako je next next tip

        }

    }

    @Override
    protected void onResume() {
        super.onResume();

    }


    ........

    @Override
    protected void onStart() {
        super.onStart();
        // Start media router discovery
        mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
                MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
    }


    //Chromecast
    /**
     * Start the receiver app
     */
    private void launchReceiver() {
        try {
            mCastListener = new Cast.Listener() {

                @Override
                public void onApplicationDisconnected(int errorCode) {
                    Log.d(TAG, "application has stopped");
                    teardown(true);
                }

            };
            // Connect to Google Play services
            mConnectionCallbacks = new ConnectionCallbacks();
            mConnectionFailedListener = new ConnectionFailedListener();
            Cast.CastOptions.Builder apiOptionsBuilder = Cast.CastOptions
                    .builder(mSelectedDevice, mCastListener);
            mApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Cast.API, apiOptionsBuilder.build())
                    .addConnectionCallbacks(mConnectionCallbacks)
                    .addOnConnectionFailedListener(mConnectionFailedListener)
                    .build();

            mApiClient.connect();
        } catch (Exception e) {
            Log.e(TAG, "Failed launchReceiver", e);
        }
    }

    /**
     * Google Play services callbacks
     */
    private class ConnectionCallbacks implements GoogleApiClient.ConnectionCallbacks {

        @Override
        public void onConnected(Bundle connectionHint) {
            Log.d(TAG, "onConnected");

            if (mApiClient == null) {
                // We got disconnected while this runnable was pending
                // execution.
                return;
            }

            try {
                if (mWaitingForReconnect) {
                    mWaitingForReconnect = false;

                    // Check if the receiver app is still running
                    if ((connectionHint != null)
                            && connectionHint.getBoolean(Cast.EXTRA_APP_NO_LONGER_RUNNING)) {
                        Log.d(TAG, "App  is no longer running");
                        teardown(true);
                    } else {
                        // Re-create the custom message channel
                        try {
                            Cast.CastApi.setMessageReceivedCallbacks(
                                    mApiClient,
                                    mHelloWorldChannel.get_messageBus_round(),
                                    mHelloWorldChannel);

                            Cast.CastApi.setMessageReceivedCallbacks(
                                    mApiClient,
                                    mHelloWorldChannel.get_messageBus_current_text(),
                                    mHelloWorldChannel);
                            Cast.CastApi.setMessageReceivedCallbacks(
                                    mApiClient,
                                    mHelloWorldChannel.get_messageBus_current_exercise(),
                                    mHelloWorldChannel);
                            Cast.CastApi.setMessageReceivedCallbacks(
                                    mApiClient,
                                    mHelloWorldChannel.get_messageBus_current_time(),
                                    mHelloWorldChannel);
                        } catch (IOException e) {
                            Log.e(TAG, "Exception while creating channel", e);
                        }
                    }
                } else {
                    // Launch the receiver app
                    Cast.CastApi.launchApplication(mApiClient, getString(R.string.app_id), false)
                            .setResultCallback(
                                    new ResultCallback<Cast.ApplicationConnectionResult>() {
                                        @Override
                                        public void onResult(
                                                Cast.ApplicationConnectionResult result) {
                                            Status status = result.getStatus();
                                            Log.d(TAG,
                                                    "ApplicationConnectionResultCallback.onResult:"
                                                            + status.getStatusCode());
                                            if (status.isSuccess()) {
                                                ApplicationMetadata applicationMetadata = result
                                                        .getApplicationMetadata();
                                                mSessionId = result.getSessionId();
                                                String applicationStatus = result
                                                        .getApplicationStatus();
                                                boolean wasLaunched = result.getWasLaunched();
                                                Log.d(TAG, "application name: "
                                                        + applicationMetadata.getName()
                                                        + ", status: " + applicationStatus
                                                        + ", sessionId: " + mSessionId
                                                        + ", wasLaunched: " + wasLaunched);
                                                mApplicationStarted = true;

                                                // Create the custom message
                                                // channel
                                                mHelloWorldChannel = new HelloWorldChannel();
                                                try {
                                                    Cast.CastApi.setMessageReceivedCallbacks(
                                                            mApiClient,
                                                            mHelloWorldChannel.get_messageBus_round(),
                                                            mHelloWorldChannel);

                                                    Cast.CastApi.setMessageReceivedCallbacks(
                                                            mApiClient,
                                                            mHelloWorldChannel.get_messageBus_current_text(),
                                                            mHelloWorldChannel);
                                                    Cast.CastApi.setMessageReceivedCallbacks(
                                                            mApiClient,
                                                            mHelloWorldChannel.get_messageBus_current_exercise(),
                                                            mHelloWorldChannel);
                                                    Cast.CastApi.setMessageReceivedCallbacks(
                                                            mApiClient,
                                                            mHelloWorldChannel.get_messageBus_current_time(),
                                                            mHelloWorldChannel);
                                                } catch (IOException e) {
                                                    Log.e(TAG, "Exception while creating channel",
                                                            e);
                                                }

                                                // set the initial instructions
                                                // on the receiver

                                                //Salje Talk to me text
                                                //sendMessage(getString(R.string.instructions),0);

                                            } else {
                                                Log.e(TAG, "application could not launch");
                                                teardown(true);
                                            }
                                        }
                                    });
                }
            } catch (Exception e) {
                Log.e(TAG, "Failed to launch application", e);
            }
        }

        @Override
        public void onConnectionSuspended(int cause) {
            Log.d(TAG, "onConnectionSuspended");
            mWaitingForReconnect = true;
        }
    }

    /**
     * Google Play services callbacks
     */
    private class ConnectionFailedListener implements
            GoogleApiClient.OnConnectionFailedListener {

        @Override
        public void onConnectionFailed(ConnectionResult result) {
            Log.e(TAG, "onConnectionFailed ");

            teardown(false);
        }
    }

    /**
     * Tear down the connection to the receiver
     */
    private void teardown(boolean selectDefaultRoute) {
        Log.d(TAG, "teardown");
        if (mApiClient != null) {
            if (mApplicationStarted) {
                if (mApiClient.isConnected() || mApiClient.isConnecting()) {
                    try {
                        Cast.CastApi.stopApplication(mApiClient, mSessionId);
                        if (mHelloWorldChannel != null) {

                            Cast.CastApi.removeMessageReceivedCallbacks(
                                    mApiClient,
                                    mHelloWorldChannel.get_messageBus_round());
                            Cast.CastApi.removeMessageReceivedCallbacks(
                                    mApiClient,
                                    mHelloWorldChannel.get_messageBus_current_text());
                            Cast.CastApi.removeMessageReceivedCallbacks(
                                    mApiClient,
                                    mHelloWorldChannel.get_messageBus_current_exercise());
                            Cast.CastApi.removeMessageReceivedCallbacks(
                                    mApiClient,
                                    mHelloWorldChannel.get_messageBus_current_time());

                            mHelloWorldChannel = null;
                        }
                    } catch (IOException e) {
                        Log.e(TAG, "Exception while removing channel", e);
                    }
                    mApiClient.disconnect();
                }
                mApplicationStarted = false;
            }
            mApiClient = null;
        }
        if (selectDefaultRoute) {
            mMediaRouter.selectRoute(mMediaRouter.getDefaultRoute());
        }
        mSelectedDevice = null;
        mWaitingForReconnect = false;
        mSessionId = null;
    }

    /**
     * Send a text message to the receiver
     */

    private void sendMessage(String message,int case_) {



        if (mApiClient != null && mHelloWorldChannel != null) {
            try {
                //Salje stranici message od text speacha
                if(case_ == 0){
                    Cast.CastApi.sendMessage(mApiClient, mHelloWorldChannel.get_messageBus_round(), message).setResultCallback(
                            new ResultCallback<Status>() {
                                @Override
                                public void onResult(Status result) {
                                    if (!result.isSuccess()) {
                                        Log.e(TAG, "Sending message failed");
                                    }
                                }
                            });
                }else if(case_ == 1){
                    Cast.CastApi.sendMessage(mApiClient, mHelloWorldChannel.get_messageBus_current_text(), message).setResultCallback(
                            new ResultCallback<Status>() {
                                @Override
                                public void onResult(Status result) {
                                    if (!result.isSuccess()) {
                                        Log.e(TAG, "Sending message failed");
                                    }
                                }
                            });
                }else if(case_ == 2){
                    Cast.CastApi.sendMessage(mApiClient, mHelloWorldChannel.get_messageBus_current_exercise(), message).setResultCallback(
                            new ResultCallback<Status>() {
                                @Override
                                public void onResult(Status result) {
                                    if (!result.isSuccess()) {
                                        Log.e(TAG, "Sending message failed");
                                    }
                                }
                            });
                }else if(case_ == 3){
                    Cast.CastApi.sendMessage(mApiClient, mHelloWorldChannel.get_messageBus_current_time(), message).setResultCallback(
                            new ResultCallback<Status>() {
                                @Override
                                public void onResult(Status result) {
                                    if (!result.isSuccess()) {
                                        Log.e(TAG, "Sending message failed");
                                    }
                                }
                            });
                }

            } catch (Exception e) {
                Log.e(TAG, "Exception while sending message", e);
            }
        } else {
            //Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Custom message channel
     */
    class HelloWorldChannel implements Cast.MessageReceivedCallback {

        /**
         * @return custom namespace
         */


        public String get_messageBus_round(){
            return getString(R.string.messageBus_round);
        }
        public String get_messageBus_current_text(){
            return getString(R.string.messageBus_current_text);
        }
        public String get_messageBus_current_exercise(){
            return getString(R.string.messageBus_current_exercise);
        }
        public String get_messageBus_current_time(){
            return getString(R.string.messageBus_current_time);
        }

        /*
         * Receive message from the receiver app
         */
        @Override
        public void onMessageReceived(CastDevice castDevice, String namespace,
                                      String message) {
            Log.d(TAG, "onMessageReceived: " + message);
        }

    }

}

问题出在菜单中,菜单应如下所示:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.casthelloworld.MainActivity" >


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