Ray*_*ito 2 android android-fragments android-navigation android-navigationview
免责声明:我已经检查了文档,因为2.1.0导航组件支持对话框片段。(https://developer.android.com/jetpack/androidx/releases/navigation#2.1.0)
DialogFragment尝试从 a 转到my时出现此错误Start Destination:
java.lang.IllegalStateException: Fragment PostDistressDialog{829f5d1} (bbbc4926-684b-491b-9772-e0f0ffebe0af)} not associated with a fragment manager.
PostDistressDialog是使用导航组件DialogFragment调用的JournalEntryFragment(可以在下面的地图中看到)。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());
}
Run Code Online (Sandbox Code Playgroud)
}
哪里thispublic class PostDistressDialog extends DialogFragment
<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>
Run Code Online (Sandbox Code Playgroud)
DialogFragment当您添加的回调被触发时,AlertDialog 会自动关闭对话框(因此删除您的) setPositiveButton。因为您正在异步工作,所以databaseSavingCompleted在 DialogFragment 被销毁、与 FragmentManager 分离并从 NavController 中删除之后调用您的方法 - 您泄漏了对 DialogFragment 的引用(否则它会被垃圾收集)。
因此,当NavHostFragment.findNavController(this)发生火灾时,所有让它能够接触到的钩子都NavController已经被清理干净了。
如果您不希望按钮立即关闭对话框,则需要null在setPositiveButton()创建对话框后通过调用其getButton()API 并手动设置一个OnClickListener将启动AsyncTask(并禁用按钮以防止多次单击)。
| 归档时间: |
|
| 查看次数: |
7961 次 |
| 最近记录: |