我正在尝试在使用androidX的应用程序中进行设置。
我的问题是,当我尝试使用PlaceAutocompleteFragment时,我得到了错误,因为它是来自android.app.fragment的片段,而我的父片段是一个androidx片段:androidx.fragment.app.Fragment,所以它使用androidx.fragment.app.FragmentManager而不是android.app.FragmentManager。
如何在androidX中处理“旧”片段?
发布于 2018-11-24 22:00:42
将这个添加到依赖项中:implementation 'androidx.appcompat:appcompat:1.0.2'.
import androidx.fragment.app.FragmentTransaction;.
Activity to AppCompatActivity。getFragmentManager()更改为getSupportFragmentManager()。这将解决您的问题。
发布于 2018-09-13 21:47:16
如果你正在使用新的库,那么只使用那些库,不要将它们组合在一起,因为你会遇到更多的问题。现在,对于您的问题,请转到您的片段,只需更改导入表单:
import android.app.fragment至:
import androidx.fragment.app.Fragment这应该可以解决你的问题。
发布于 2020-01-31 18:51:33
不需要直接从Activity获取FragmentManager,因为Fragment中提供了一个名为getParentFragmentManager的替换方法
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;来自不推荐使用的getFragmentManager的文档
* @deprecated This has been removed in favor of <code>getParentFragmentManager()</code> which
* throws an {@link IllegalStateException} if the FragmentManager is null. Check if
* {@link #isAdded()} returns <code>false</code> to determine if the FragmentManager is
* <code>null</code>.替换方法getParentFragmentManager的文档
* Return the FragmentManager for interacting with fragments associated
* with this fragment's activity. Note that this will available slightly
* before {@link #getActivity()}, during the time from when the fragment is
* placed in a {@link FragmentTransaction} until it is committed and
* attached to its activity.
*
* <p>If this Fragment is a child of another Fragment, the FragmentManager
* returned here will be the parent's {@link #getChildFragmentManager()}.
*
* @throws IllegalStateException if not associated with a transaction or host.只需注意检查isAdded以确保FragmentManager不为空。
https://stackoverflow.com/questions/52256959
复制相似问题