Home » Java » Caused by: java.lang.IllegalStateException: Failed to initialize CastContext

Caused by: java.lang.IllegalStateException: Failed to initialize CastContext

Posted by: admin July 12, 2018 Leave a comment

Questions:

I tried to develop android chrome cast sender application. I added to cast option provider to manifest and all methods are public.
Here is my code :
Manifest

<meta-data android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value=".util.CastOptionsProvider" />

This is my cast option provider

`
public class CastOptionsProvider implements OptionsProvider {

@Override
public CastOptions getCastOptions(Context context) {
    NotificationOptions notificationOptions = new NotificationOptions.Builder()
            .setActions(Arrays.asList(MediaIntentReceiver.ACTION_SKIP_NEXT,
                    MediaIntentReceiver.ACTION_TOGGLE_PLAYBACK,
                    MediaIntentReceiver.ACTION_STOP_CASTING), new int[]{1, 2})
            .setTargetActivityClassName(ExpandedControlsActivity.class.getName())
            .build();
    CastMediaOptions mediaOptions = new CastMediaOptions.Builder()
            .setImagePicker(new ImagePickerImpl())
            .setNotificationOptions(notificationOptions)
            .setExpandedControllerActivityClassName(ExpandedControlsActivity.class.getName())
            .build();
    return new CastOptions.Builder()
            .setReceiverApplicationId(context.getString(R.string.chrome_cast_id))
            .setCastMediaOptions(mediaOptions)
            .build();
}

@Override
public List<SessionProvider> getAdditionalSessionProviders(Context appContext) {
    return null;
}

}
`

This is my player class

`public class PlayerActivity extends AppCompatActivity implements 
 OnBufferingUpdateListener {

 @Override
 public void onCreate(Bundle paramBundle) {
    super.onCreate(paramBundle);
    setContentView(R.layout.activity_player);

    initView();

    setupCastListener();// listener initializing method

    //for Google chrome casting
    mCastContext = CastContext.getSharedInstance(this);
    mCastSession = mCastContext.getSessionManager().getCurrentCastSession();

`

But Cannot run.. provided failed to initialize castContext Error. look down.. ==>

` Caused by: java.lang.IllegalStateException: Failed to initialize CastContext.
                                                                  at com.google.android.gms.cast.framework.CastContext.zzbu(Unknown Source)
                                                                  at com.google.android.gms.cast.framework.CastContext.getSharedInstance(Unknown Source)
                                                                  at lk.bhasha.mobitv.activities.PlayerActivity.initOthers(PlayerActivity.java:408)
                                                                  at lk.bhasha.mobitv.activities.PlayerActivity.onCreate(PlayerActivity.java:194)
                                                                  at android.app.Activity.performCreate(Activity.java:6915)
                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) 
                                                                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
                                                                  at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                  at android.os.Looper.loop(Looper.java:156) 
                                                                  at android.app.ActivityThread.main(ActivityThread.java:6523) 
                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) 
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832) 
                                                               Caused by: java.lang.ClassNotFoundException: Invalid name: .util.CastOptionsProvider

please help me.

Answers: