2012-05-16 59 views
0

我有一個活動和兩個片段。其中一個片段擴展DialogFragment並顯示一些文本。 在我的活動我已經重寫onCreate方法:DialogFragment方向變化

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(layoutId); 
    if (savedInstanceState == null) 
     initFragment(); 
} 

initFragment()添加到一個的FrameLayout片段在我layoutId。如果沒有顯示DialogFragment,則一切正常,但如果顯示DialogFragment實例 - 方向更改會拋出IllegalStateException,該片段未返回視圖「setContentView」。

BTW我使用ActionBarSherlock

E/AndroidRuntime(19517): FATAL EXCEPTION: main 
E/AndroidRuntime(19517): java.lang.RuntimeException: Unable to start activity Co 
mponentInfo{ru.nixan.android/ru.nixan.android.Cards}: android.view.InflateException: B 
inary XML file line #30: Error inflating class <unknown> 
E/AndroidRuntime(19517):  at android.app.ActivityThread.performLaunchActiv 
ity(ActivityThread.java:1956) 
E/AndroidRuntime(19517):  at android.app.ActivityThread.handleLaunchActivi 
ty(ActivityThread.java:1981) 
E/AndroidRuntime(19517):  at android.app.ActivityThread.handleRelaunchActi 
vity(ActivityThread.java:3351) 
E/AndroidRuntime(19517):  at android.app.ActivityThread.access$700(Activit 
yThread.java:123) 
E/AndroidRuntime(19517):  at android.app.ActivityThread$H.handleMessage(Ac 
tivityThread.java:1151) 
E/AndroidRuntime(19517):  at android.os.Handler.dispatchMessage(Handler.ja 
va:99) 
E/AndroidRuntime(19517):  at android.os.Looper.loop(Looper.java:137) 
E/AndroidRuntime(19517):  at android.app.ActivityThread.main(ActivityThrea 
d.java:4424) 
E/AndroidRuntime(19517):  at java.lang.reflect.Method.invokeNative(Native 
Method) 
E/AndroidRuntime(19517):  at java.lang.reflect.Method.invoke(Method.java:5 
11) 
E/AndroidRuntime(19517):  at com.android.internal.os.ZygoteInit$MethodAndA 
rgsCaller.run(ZygoteInit.java:784) 
E/AndroidRuntime(19517):  at com.android.internal.os.ZygoteInit.main(Zygot 
eInit.java:551) 
E/AndroidRuntime(19517):  at dalvik.system.NativeStart.main(Native Method) 

E/AndroidRuntime(19517): Caused by: android.view.InflateException: Binary XML fi 
le line #30: Error inflating class <unknown> 
E/AndroidRuntime(19517):  at android.view.LayoutInflater.createView(Layout 
Inflater.java:606) 
E/AndroidRuntime(19517):  at android.view.LayoutInflater.createViewFromTag 
(LayoutInflater.java:680) 
E/AndroidRuntime(19517):  at android.view.LayoutInflater.rInflate(LayoutIn 
flater.java:739) 
E/AndroidRuntime(19517):  at android.view.LayoutInflater.rInflate(LayoutIn 
flater.java:742) 
E/AndroidRuntime(19517):  at android.view.LayoutInflater.inflate(LayoutInf 
later.java:489) 
E/AndroidRuntime(19517):  at android.view.LayoutInflater.inflate(LayoutInf 
later.java:396) 
E/AndroidRuntime(19517):  at android.view.LayoutInflater.inflate(LayoutInf 
later.java:352) 
E/AndroidRuntime(19517):  at com.android.internal.policy.impl.PhoneWindow. 
generateLayout(PhoneWindow.java:2707) 
E/AndroidRuntime(19517):  at com.android.internal.policy.impl.PhoneWindow. 
installDecor(PhoneWindow.java:2767) 
E/AndroidRuntime(19517):  at com.android.internal.policy.impl.PhoneWindow. 
setContentView(PhoneWindow.java:247) 
E/AndroidRuntime(19517):  at com.actionbarsherlock.internal.ActionBarSherl 
ockNative.setContentView(ActionBarSherlockNative.java:119) 
E/AndroidRuntime(19517):  at com.actionbarsherlock.app.SherlockFragmentAct 
ivity.setContentView(SherlockFragmentActivity.java:309) 
E/AndroidRuntime(19517):  at ru.nixan.android.Cards.onCreate(Cards.java:53) 
E/AndroidRuntime(19517):  at android.app.Activity.performCreate(Activity.j 
ava:4465) 
E/AndroidRuntime(19517):  at android.app.Instrumentation.callActivityOnCre 
ate(Instrumentation.java:1049) 
E/AndroidRuntime(19517):  at android.app.ActivityThread.performLaunchActiv 
ity(ActivityThread.java:1920) 
E/AndroidRuntime(19517):  ... 12 more 
E/AndroidRuntime(19517): Caused by: java.lang.reflect.InvocationTargetException 
E/AndroidRuntime(19517):  at java.lang.reflect.Constructor.constructNative 
(Native Method) 
E/AndroidRuntime(19517):  at java.lang.reflect.Constructor.newInstance(Con 
structor.java:417) 
E/AndroidRuntime(19517):  at android.view.LayoutInflater.createView(Layout 
Inflater.java:586) 
E/AndroidRuntime(19517):  ... 27 more 
E/AndroidRuntime(19517): Caused by: android.view.InflateException: Binary XML fi 
le line #7: Error inflating class fragment 
E/AndroidRuntime(19517):  at android.view.LayoutInflater.createViewFromTag 
(LayoutInflater.java:697) 
E/AndroidRuntime(19517):  at android.view.LayoutInflater.rInflate(LayoutIn 
flater.java:739) 
E/AndroidRuntime(19517):  at android.view.LayoutInflater.inflate(LayoutInf 
later.java:489) 
E/AndroidRuntime(19517):  at android.view.LayoutInflater.inflate(LayoutInf 
later.java:396) 
E/AndroidRuntime(19517):  at com.android.internal.widget.ActionBarView.<in 
it>(ActionBarView.java:236) 
E/AndroidRuntime(19517):  ... 30 more 
E/AndroidRuntime(19517): Caused by: java.lang.IllegalStateException: Fragment ru 
.nixan.android.fragments.BalanceFragment did not create a view. 
E/AndroidRuntime(19517):  at android.support.v4.app.FragmentActivity.onCre 
ateView(FragmentActivity.java:293) 
E/AndroidRuntime(19517):  at android.view.LayoutInflater.createViewFromTag 
(LayoutInflater.java:669) 
E/AndroidRuntime(19517):  ... 34 more 
W/ActivityManager(30340): Force finishing activity ru.nixan.android/.Cards 

ErrorDialog.java

public class ErrorDialog extends SherlockDialogFragment implements 
     OnClickListener { 

    public static final String FRAGMENT_TAG = "error"; 

    public static final String EXTRA_EXCEPTION = "exception"; 

    public static ErrorDialog getInstance(Exception excpetion) { 
     ErrorDialog result = new ErrorDialog(); 
     Bundle data = new Bundle(); 
     data.putSerializable(EXTRA_EXCEPTION, excpetion); 
     result.setArguments(data); 
     return result; 
    } 

    // some methods that are not in lifecycle are not presented here. 

    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     setRetainInstance(true); 
     setShowsDialog(true); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setTitle(R.string.dialogErrorTitle); 
     builder.setPositiveButton(R.string.dialogDismissButton, this); 
     Exception exception = (Exception) getArguments().getSerializable(
       EXTRA_EXCEPTION); 
     builder.setMessage(getErrorMessage(exception, getActivity())); 
     return builder.create(); 
    } 
} 

活動:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_cards); 
    if (savedInstanceState == null) { 
     Uri data = getIntent().getData(); 
     if (data == null || PATH_MENU.equals(data.getLastPathSegment())) 
      initMenuFragment(); 
     else if (PATH_CARDS.equals(data.getLastPathSegment())) 
      initCardsFragment(); 
    } 
} 

private void showError(Exception exception) { 
    ErrorDialog.getInstance(exception).show(getSupportFragmentManager()); 

} 
+0

你可以發佈整個異常和更多的代碼嗎? – Ostkontentitan

+0

@Konstantin done – nixan

回答

1

我在我自己的實現完全相同的情況。 你可以看看我在這裏提出的解決方法:https://stackoverflow.com/a/14774821/1894568

基本上它不使用layout-xml中的片段,因爲生命週期不同於通過FragmentManager添加的片段。而是將特定於方向的片段(LandscapeModeFragment & PortraitModeFragment)注入到FrameLayout容器中。

+0

這是現貨。 –