当使用mvvm实现启动屏幕时出现此错误

时间:2018-08-23 09:52:30

标签: android 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;
            }
        }
    };

}

ViewModel类:

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.invokeNative(本机方法)           在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.lang.ClassCastException:com.careerlift.ViewModel.SplashViewModel无法转换为   java.util.concurrent.Executor           在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(本机方法)           在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(本机方法)

1 个答案:

答案 0 :(得分:0)

使用此 .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());
                    }
                }
            });