有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 java。lang.IllegalArgumentException:本地类和匿名类不能是ViewModels

我们正在使用Firebase Crashlytics从生产环境接收一些异常。不幸的是,这个问题在本地是不可复制的,在四处搜索时,我找不到相同的异常消息,except some source code,这并没有真正的帮助

编辑:如果你想知道我为什么添加Java作为标记,请往下看。你可以在没有任何Android知识的情况下回答这个问题

该应用程序使用片段(ViewPager)、多索引和lambda可运行文件,并且它不使用视图模型或数据绑定。如果有帮助的话,当应用程序处于前台时,异常似乎也会发生

从API 23(安卓6.0)到API 29(安卓Q),我们几乎在每个API级别都有至少一个异常。不过,在较低的API上没有记录任何异常

异常是从不同的点发生的,无论是从Activity.onStart(),还是从ViewPager.setAdapter()还是从Activity.onDestroy()和一些我还没有检查过的点。虽然它们都包含一些指向程序代码的点,但我不确定程序本身是否是真正的原因。不过,堆栈轨迹上有一些交点

这里有一个例外。显然,由于proguard的原因,某些部分被混淆了。(由于保密原因,包名被隐藏):

Caused by java.lang.IllegalArgumentException: Local and anonymous classes can not be ViewModels
       at 安卓x.lifecycle.ViewModelProvider.a + 40(ViewModelProvider.java:40)
       at 安卓x.loader.app.LoaderManagerImpl$LoaderViewModel.dump + 361(LoaderManagerImpl.java:361)
       at 安卓x.loader.app.LoaderManagerImpl.<init> + 5(LoaderManagerImpl.java:5)
       at 安卓x.loader.app.LoaderManager.a + 9(LoaderManager.java:9)
       at 安卓x.fragment.app.Fragment.performDestroyView + 29(Fragment.java:29)
       at 安卓x.fragment.app.FragmentManagerImpl.a + 963(FragmentManagerImpl.java:963)
       at 安卓x.fragment.app.FragmentManagerImpl.m + 99(FragmentManagerImpl.java:99)
       at 安卓x.fragment.app.FragmentManagerImpl.dump + 489(FragmentManagerImpl.java:489)
       at 安卓x.fragment.app.FragmentManagerImpl.completeShowHideFragment + 1165(FragmentManagerImpl.java:1165)
       at 安卓x.fragment.app.FragmentManagerImpl.dispatchDestroy + 2644(FragmentManagerImpl.java:2644)
       at 安卓x.fragment.app.FragmentController.c + 4(FragmentController.java:4)
       at 安卓x.fragment.app.FragmentActivity.onDestroy + 5(FragmentActivity.java:5)
       at 安卓x.appcompat.app.AppCompatActivity.onDestroy(AppCompatActivity.java)
       at com.example.myapp.activities.MainActivity.onDestroy + 28(MainActivity.java:28)
       at 安卓.app.Activity.performDestroy + 7724(Activity.java:7724)
       at 安卓.app.Instrumentation.callActivityOnDestroy + 1310(Instrumentation.java:1310)
       at 安卓.app.ActivityThread.performDestroyActivity + 4723(ActivityThread.java:4723)
       at 安卓.app.ActivityThread.handleDestroyActivity + 4761(ActivityThread.java:4761)
       at 安卓.app.servertransaction.DestroyActivityItem.execute + 39(DestroyActivityItem.java:39)
       at 安卓.app.servertransaction.TransactionExecutor.executeLifecycleState + 145(TransactionExecutor.java:145)
       at 安卓.app.servertransaction.TransactionExecutor.execute + 70(TransactionExecutor.java:70)

(MainActivity.java:28)行指向此处:

public void onDestroy() {
    super.onDestroy(); // the stack trace points here
}

它发生在super调用中,这是我无法避免的调用,也无法在try-catch中进行包装。我确实意识到所有堆栈跟踪都调用了一个名为LoaderManager(我找不到Android Jetpack文档)的东西,它在内部调用ViewModelProvider。显然,它使用视图模型提供程序来记录内容,但在某些情况下(异常率实际上很低),它会发生并被报告

在堆栈跟踪的顶部,它提到了ViewModelProvider。虽然堆栈跟踪是模糊的,但是从Fragment.performDestroyView()我能够跟踪到试图调用的方法

出于好奇,我检查了ViewModelProvider方法的源代码,发现:

/**
 * Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or
 * an activity), associated with this {@code ViewModelProvider}.
 * <p>
 * The created ViewModel is associated with the given scope and will be retained
 * as long as the scope is alive (e.g. if it is an activity, until it is
 * finished or process is killed).
 *
 * @param modelClass The class of the ViewModel to create an instance of it if it is not
 *                   present.
 * @param <T>        The type parameter for the ViewModel.
 * @return A ViewModel that is an instance of the given type {@code T}.
 */
@NonNull
@MainThread
public <T extends ViewModel> T get(@NonNull Class<T> modelClass) {
    String canonicalName = modelClass.getCanonicalName();
    if (canonicalName == null) {
        throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels");
    }
    return get(DEFAULT_KEY + ":" + canonicalName, modelClass);
}

现在,这一行:

String canonicalName = modelClass.getCanonicalName();

我不确定在哪种情况下规范名称是空的。但是,根本原因就在这里。所以,我的主要问题是,为什么类的规范名称会返回null?这是我能解决的问题吗

非常感谢您的帮助,谢谢


共 (0) 个答案