Ad

When Implement Splash Screen Using Mvvm Getting This Error

- 1 answer

Here is my code:

public class SplashActivtiy extends AppCompatActivity {

    private final static int MSG_CONTINUE = 1234;
    private final static long DELAY = 2000;
    private static final String TAG = SplashActivtiy.class.getSimpleName();
    private SplashViewModel splashViewModel;
    private LifecycleRegistry mLifecycleRegistry;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_activtiy);

       // FirebaseApp.initializeApp(this);

        splashViewModel = ViewModelProviders.of(this).get(SplashViewModel.class);

        splashViewModel.retrieveRemoteConfig(SplashActivtiy.this);

        mHandler.sendEmptyMessageDelayed(MSG_CONTINUE, DELAY);
    }
    @Override
    protected void onDestroy() {
        mHandler.removeMessages(MSG_CONTINUE);
        super.onDestroy();
    }


    private void _continue() {
        startActivity(new Intent(this, Swipe.class));

        finish();
    }

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MSG_CONTINUE:
                    _continue();
                    break;
            }
        }
    };

}

ViewModel Class :

public class SplashViewModel extends ViewModel {

    private static final String TAG = SplashViewModel.class.getSimpleName();
    FirebaseRemoteConfig mFirebaseRemoteConfig;

    public void retrieveRemoteConfig(Context context) {
        Log.d(TAG, "retrieveRemoteConfig: ");
        FirebaseApp.initializeApp(context);

        mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();


        mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults);

        // cacheExpirationSeconds is set to cacheExpiration here, indicating the next fetch request
        // will use fetch data from the Remote Config service, rather than cached parameter values,
        // if cached parameter values are more than cacheExpiration seconds old.
        // See Best Practices in the README for more information.

        final FirebaseRemoteConfig finalMFirebaseRemoteConfig = mFirebaseRemoteConfig;
        mFirebaseRemoteConfig.fetch()
                .addOnCompleteListener((Executor) this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Log.d(TAG, "onComplete: ");
                        if (task.isSuccessful()) {
                            Log.d(TAG, "onComplete: Remote config fetched successfully");
                            //  Toast.makeText(SplashActivity.this, "Fetch Succeeded",Toast.LENGTH_SHORT).show();
                            // After config data is successfully fetched, it must be activated before newly fetched
                            // values are returned.
                            finalMFirebaseRemoteConfig.activateFetched();
                        } else {
                            //  Toast.makeText(SplashActivity.this, "Fetch Failed",Toast.LENGTH_SHORT).show();
                            Log.e(TAG, "onComplete: Remote config fetching unsuccessful " + task.getException());
                        }
                    }
                });
    }
}

Process: com.careerlift, PID: 13620 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.careerlift/com.careerlift.SplashActivtiy}: java.lang.ClassCastException: com.careerlift.ViewModel.SplashViewModel cannot be cast to java.util.concurrent.Executor at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5021) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassCastException: com.careerlift.ViewModel.SplashViewModel cannot be cast to java.util.concurrent.Executor at com.careerlift.ViewModel.SplashViewModel.retrieveRemoteConfig(SplashViewModel.java:36) at com.careerlift.SplashActivtiy.onCreate(SplashActivtiy.java:31) at android.app.Activity.performCreate(Activity.java:5397) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)  at android.app.ActivityThread.access$800(ActivityThread.java:135)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5021)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)  at dalvik.system.NativeStart.main(Native Method)

Ad

Answer

Use this.addOnCompleteListener(context, new OnCompleteListener<Void>()

Instead of.addOnCompleteListener((Executor) this, new OnCompleteListener<Void>()

SAMPLE CODE

final FirebaseRemoteConfig finalMFirebaseRemoteConfig = mFirebaseRemoteConfig;
    mFirebaseRemoteConfig.fetch()
            .addOnCompleteListener(context, new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    Log.d(TAG, "onComplete: ");
                    if (task.isSuccessful()) {
                        Log.d(TAG, "onComplete: Remote config fetched successfully");
                        //  Toast.makeText(SplashActivity.this, "Fetch Succeeded",Toast.LENGTH_SHORT).show();
                        // After config data is successfully fetched, it must be activated before newly fetched
                        // values are returned.
                        finalMFirebaseRemoteConfig.activateFetched();
                    } else {
                        //  Toast.makeText(SplashActivity.this, "Fetch Failed",Toast.LENGTH_SHORT).show();
                        Log.e(TAG, "onComplete: Remote config fetching unsuccessful " + task.getException());
                    }
                }
            });
Ad
source: stackoverflow.com
Ad