我有一个活动和两个片段。其中一个片段扩展了 DialogFragment 并显示了一些文本。在我的活动中,我已经覆盖了 onCreate 方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layoutId);
if (savedInstanceState == null)
initFragment();
}
initFragment() 将片段添加到我的 layoutId 中的 FrameLayout。如果没有显示 DialogFragment,一切正常,但如果显示 DialogFragment 实例 - 方向更改会引发 IllegalStateException,该片段未在“setContentView”行返回视图。
顺便说一句,我正在使用 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
错误对话框.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());
}