【发布时间】:2020-02-26 09:18:53
【问题描述】:
免责声明:我已经检查了文档,并且自 2.1.0 以来,导航组件已支持对话框片段。 (https://developer.android.com/jetpack/androidx/releases/navigation#2.1.0)
我遇到的错误
我在尝试从 DialogFragment 转到我的 Start Destination 时遇到此错误:
java.lang.IllegalStateException: Fragment PostDistressDialog{829f5d1} (bbbc4926-684b-491b-9772-e0f0ffebe0af)} not associated with a fragment manager.
PostDistressDialog 是使用导航组件从JournalEntryFragment(可以在下面的地图中看到)调用的DialogFragment。 PostDistressDialog 不是 JournalEntryFragment 的内部类。它属于自己的扩展类DialogFragment
我的导航图的图片
函数调用导航控制器
public class PostDistressDialog extends DialogFragment implements ISaveDatabase {
...
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
if (getArguments()!=null) {
...
// Set up the Alert Dialog
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext());
alertDialog.setTitle(R.string.distressed_levels);
alertDialog.setMessage(R.string.distressed_how_feel_post);
// Inflate and set the layout for the dialog
View layout = View.inflate(getActivity(), R.layout.dialog_seekbar, null);
alertDialog.setView(layout);
....
// Add okay button
alertDialog.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Save post distress value in Journal Entry
mJournalEntry.setPostDistress(mTempDistressValue);
// Save to Journal Entry to database
// Check if journal entry empty
if(isJournalEntryEmpty(mJournalEntry)){
...
}
else{
// Give title if empty
if(mJournalEntry.getTitle().isEmpty()) {
....
// Save to database
new SaveDatabase(getContext(),PostDistressDialog.this).execute(mJournalEntry);
}
// Go to main menu
}
});
return alertDialog.create();
}
return null;
}
...
@Override
public void databaseSavingCompleted(){
NavHostFragment.findNavController(this).navigate(PostDistressDialogDirections.postDistressDialogToJournalListAction());
}
}
this 是 public class PostDistressDialog extends DialogFragment
我的导航 XML 文件中的对话框
<dialog
android:id="@+id/postDistressDialog"
android:name="com.dgrullon.cbtjourney.dialogs.PostDistressDialog"
android:label="PostDistressDialog" >
<argument
android:name="postDistressDialogArguments"
app:argType="com.dgrullon.cbtjourney.pojo.JournalEntries"/>
<action
android:id="@+id/postDistressDialog_to_journalListAction"
app:destination="@id/journalList"
app:popUpTo="@id/journalList"
app:popUpToInclusive="true" />
</dialog>
【问题讨论】:
-
“未与片段管理器关联”表示您的片段不再附加到片段管理器。你在哪里打电话
NavHostFragment.findNavController(this)? -
感谢@ianhanniballake 的快速回复。我编辑了我的答案以显示更多我的
DialogFragments 代码。我在其onPostExecute()方法内的另一个AsyncTask类中调用NavHostFragment.findNavController(this)。导航组件不是取消片段管理器和导航组件“管理”所有片段吗?
标签: android android-fragments android-navigation android-navigationview