gpt4 book ai didi

android - DialogFragment 崩溃中的 setRetainInstance

转载 作者:行者123 更新时间:2023-11-30 00:43:40 25 4
gpt4 key购买 nike

我有一个 DialogFragment 附加到 Activity,当屏幕旋转时,Activity 被销毁,DialogFragment 也被销毁,我想保留 DialogFragment 和我的DialogFragment 代码如下,我加了setRetainInstance(true);会崩溃

@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(sClassTag, "onCreate");
super.onCreate(savedInstanceState);
setRetainInstance(true);
int style = DialogFragment.STYLE_NORMAL, theme = 0;
setStyle(style, theme);

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(sClassTag, "onCreateView");
if(mView==null){
mView = inflater.inflate(R.layout.layout_password,container,false);
}
addButtonListener();
addEdittextListener();
return mView;
}

我在这里获取崩溃日志

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.microsoft.pdfviewertestapplication/com.microsoft.pdfviewertestapplication.MainActivity}: java.lang.IllegalStateException: DialogFragment can not be attached to a container view
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

您的 Activity 是否扩展了 FragmentActivity 并且您是否在 Manifest 文件中为 Activity 设置了 configChanges

将父 Activity 作为 FragmentActivity 并将 fragment 膨胀为

DialogFragment newFragment = new Diafragment();
newFragment.show(getSupportFragmentManager(), "dialog");

关于android - DialogFragment 崩溃中的 setRetainInstance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42152791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com