required android.app.fragment,androidx.fragment.app.FragmentManager获取问题
时间: 2024-05-04 12:20:44 浏览: 177
如果你需要使用 `android.app.Fragment` 和 `android.app.FragmentManager`,则需要使用 Android API 28或更低版本。但是,如果你使用的是 Android API 29或更高版本,则应使用 `androidx.fragment.app.Fragment` 和 `androidx.fragment.app.FragmentManager`。
如果你的项目中使用的是 AndroidX 库,则可以在你的 build.gradle 文件中添加以下依赖项:
```
implementation 'androidx.fragment:fragment:1.3.0'
```
然后,你可以在你的代码中使用 `androidx.fragment.app.Fragment` 和 `androidx.fragment.app.FragmentManager` 来获取你需要的对象。如果你仍然需要使用 `android.app.Fragment` 和 `android.app.FragmentManager`,你可以将 `androidx.fragment.app.Fragment` 转换为 `android.app.Fragment`,并将 `androidx.fragment.app.FragmentManager` 转换为 `android.app.FragmentManager`。
例如:
```
// 获取 androidx.fragment.app.Fragment 对象
val myFragment = MyFragment()
// 将 androidx.fragment.app.Fragment 转换为 android.app.Fragment
val fragment = myFragment as android.app.Fragment
// 获取 androidx.fragment.app.FragmentManager 对象
val fragmentManager = supportFragmentManager
// 将 androidx.fragment.app.FragmentManager 转换为 android.app.FragmentManager
val fragmentManager = fragmentManager as android.app.FragmentManager
```
这样,你就可以在 AndroidX 和旧版本之间无缝切换了。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)