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;
}
}
};
}
视图模型类:
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)主要(本机方法)主要(本机方法)
解决方案
用这个 .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());
}
}
});
推荐阅读
- vb.net - DataGridView 中的 ctrl F
- c# - 从 C# 中的字符串变量解析 XML 命名空间
- python - 如何让我的对象在 Pygame 中移动时不留下痕迹?
- go - MapR 5.2.2 客户端
- keras - 隐藏层中的大量神经元产生更好的结果,为什么?
- .net-core - 创建一个包含 3 个表中的列的视图
- typescript - 如何在 eslint 中使用 atom-typescript 或替代方法
- charts - ApexCharts:隐藏图表中的每第 n 个标签
- android - 如何修复“未安装应用程序” - Xamarin 应用程序分发
- sql - 统计多张表的普通用户数