取代过时的BrowseFragment AndroidTV

问题描述 投票:1回答:1

此类已在API级别27.1.0中弃用。使用BrowseSupportFragment

但是当我替换了这个

public class MainFragment extends BrowseFragment 

to

public class MainFragment extends BrowseSupportFragment

发生波纹管异常

原因:android.app.Fragment $ InstantiationException:试图实例化不是Fragment的类MainFragment

我的XML代码是

<?xml version="1.0" encoding="utf-8"?>
  <fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:name="com.example.tvapplocation1.MainFragment"
  android:layout_width="match_parent" android:layout_height="match_parent">
</fragment>
androidx android-tv leanback
1个回答
1
投票

简短的答案:如果您的活动未从FragmentActivity扩展,则可能会发生。

更长的答案:Fragment类有两种版本,一种内置在操作系统中,另一种是support / Android X库()的一部分。您应该始终使用support / Android X版本,因为它提供了跨Android OS版本的兼容性和一致的行为。各种* SupportFragment类(例如BrowseSupportFragment)从Fragment的support / Android X版本扩展而来,这要求您使用support / Android X库中的FragmentActivity。

© www.soinside.com 2019 - 2024. All rights reserved.