我有一個DialogFragment
連接到活動中,並且當屏幕旋轉時,活動被破壞,這樣做的DialogFragment
,我想保留DialogFragment
和我DialogFragment
代碼如下,我添加setRetainInstance(true);
它會崩潰setRetainInstance在DialogFragment崩潰
@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中調用DialogFragment? –
聽起來像你倒下了許多碎片陷阱。我遇到了同樣的問題,我可以通過閱讀這篇優秀的文章來解決這個問題:http://code.hootsuite.com/orientation-changes-on-android/。 –