首页 > 解决方案 > 使用 mvvm 实现启动画面时出现此错误

问题描述

这是我的代码:

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

}

视图模型类:

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());
                        }
                    }
                });
    }
}

进程:com.careerlift,PID:13620 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.careerlift/com.careerlift.SplashActivtiy}:java.lang.ClassCastException:com.careerlift.ViewModel.SplashViewModel 无法转换为 java .util.concurrent.Executor 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 在 android.app.ActivityThread.access$800(ActivityThread.java: 135) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5021) 在 java.lang.reflect.Method。在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 在 com.android.internal 的 java.lang.reflect.Method.invoke(Method.java:515) 的 invokeNative(Native Method)。 os.ZygoteInit.main(ZygoteInit.java:643) at dalvik.system.NativeStart.main(Native Method) 原因:java.lang.ClassCastException: com.careerlift.ViewModel.SplashViewModel 无法转换为 java.util.concurrent。执行者在 com.careerlift.ViewModel.SplashViewModel.retrieveRemoteConfig(SplashViewModel.java:36) 在 com.careerlift.SplashActivtiy.onCreate(SplashActivtiy.java:31) 在 android.app.Activity.performCreate(Activity.java:5397) 在 android .app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) 在 android.app.ActivityThread。performLaunchActivity(ActivityThread.java:2159) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 在 android.app.ActivityThread.access$800(ActivityThread.java:135) 在 android.app.ActivityThread$H.handleMessage( ActivityThread.java:1196) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:136) 在 android.app.ActivityThread.main(ActivityThread.java:5021 ) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java :827) 在 dalvik.system.NativeStart.main(Native Method) 的 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)2159) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 在 android.app.ActivityThread.access$800(ActivityThread.java:135) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:136) 在 android.app.ActivityThread.main(ActivityThread.java:5021) 在 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. dalvik.system.NativeStart.main(本机方法)上的 android.internal.os.ZygoteInit.main(ZygoteInit.java:643)2159)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)在android.app.ActivityThread.access$800(ActivityThread.java:135)在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:136) 在 android.app.ActivityThread.main(ActivityThread.java:5021) 在 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. dalvik.system.NativeStart.main(本机方法)上的 android.internal.os.ZygoteInit.main(ZygoteInit.java:643)handleLaunchActivity(ActivityThread.java:2245) 在 android.app.ActivityThread.access$800(ActivityThread.java:135) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 在 android.os.Handler.dispatchMessage( Handler.java:102) 在 android.os.Looper.loop(Looper.java:136) 在 android.app.ActivityThread.main(ActivityThread.java:5021) 在 java.lang.reflect.Method.invokeNative(Native Method)在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:643)在 dalvik.system.NativeStart.main(本机方法)handleLaunchActivity(ActivityThread.java:2245) 在 android.app.ActivityThread.access$800(ActivityThread.java:135) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 在 android.os.Handler.dispatchMessage( Handler.java:102) 在 android.os.Looper.loop(Looper.java:136) 在 android.app.ActivityThread.main(ActivityThread.java:5021) 在 java.lang.reflect.Method.invokeNative(Native Method)在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:643)在 dalvik.system.NativeStart.main(本机方法)java:135) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:136 ) 在 android.app.ActivityThread.main(ActivityThread.java:5021) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 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)java:135) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:136 ) 在 android.app.ActivityThread.main(ActivityThread.java:5021) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com。 android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 在 dalvik.system.NativeStart.main(Native Method)136) 在 android.app.ActivityThread.main(ActivityThread.java:5021) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 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)136) 在 android.app.ActivityThread.main(ActivityThread.java:5021) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 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)主要(本机方法)主要(本机方法)

标签: androidmvvm

解决方案


用这个 .addOnCompleteListener(context, new OnCompleteListener<Void>()

代替 .addOnCompleteListener((Executor) this, new OnCompleteListener<Void>()

示例代码

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());
                    }
                }
            });

推荐阅读