Update api-exception documentation

This commit is contained in:
2022-12-29 20:39:05 +08:00
parent eb5abb3403
commit 84e7861bb8
2 changed files with 66 additions and 2 deletions

View File

@@ -1917,4 +1917,36 @@ loadSystem {
**Solution**
Since the cache will store the found `Class` name in `SharedPreferences`, but the data directory does not exist in the System Framework, so please do not use this function in the System Framework.
Since the cache will store the found `Class` name in `SharedPreferences`, but the data directory does not exist in the System Framework, so please do not use this function in the System Framework.
###### exception
::: danger IllegalStateException
Target Class type cannot cast to **TYPE**
:::
**Abnormal**
Wrong type declared when converting a string class name to the target `Class` using the `toClass` method.
> The following example
```kotlin
// Assume the target type is Activity but it was wrongly cast to WrongClass type
val clazz = "android.app.Activity".toClass<WrongClass>()
```
**Solution**
> The following example
```kotlin
// <Solution 1> Fill in the correct type
val clazz1 = "android.app.Activity".toClass<Activity>()
// <Solution 2> Do not fill in the generic declaration
val clazz2 = "android.app.Activity".toClass()
```
Please ensure that the generic type declared after the `toClass` method is the specified target `Class` type, and you do not need to fill in the generic declaration if the target type is not sure.

View File

@@ -1848,4 +1848,36 @@ loadSystem {
**解决方案**
由于缓存会将找到的 `Class` 名称存入 `SharedPreferences`,但是系统框架不存在 data 目录,所以请不要在系统框架中使用此功能。
由于缓存会将找到的 `Class` 名称存入 `SharedPreferences`,但是系统框架不存在 data 目录,所以请不要在系统框架中使用此功能。
###### exception
::: danger IllegalStateException
Target Class type cannot cast to **TYPE**
:::
**异常原因**
使用 `toClass` 方法将字符串类名转换为目标 `Class` 时声明了错误的类型。
> 示例如下
```kotlin
// 假设目标类型是 Activity 但是被错误地转换为了 WrongClass 类型
val clazz = "android.app.Activity".toClass<WrongClass>()
```
**解决方案**
> 示例如下
```kotlin
// <解决方案 1> 填写正确的类型
val clazz1 = "android.app.Activity".toClass<Activity>()
// <解决方案 2> 不填写泛型声明
val clazz2 = "android.app.Activity".toClass()
```
请确保 `toClass` 方法后声明的泛型是指定的目标 `Class` 类型,在不确定目标类型的情况下你可以不需要填写泛型声明。