我使用Eclipse编译应用程序,我曾经得到这个错误,但不是很多。但是因为我已经切换到android工作室,所以我遇到了很多崩溃。它几乎每台设备都崩溃了。我已经尝试过做一切但似乎无法解决这个问题。我尝试过使用getfragmentManager()和getSupportFragmentManager(),两者都崩溃了。不知道现在该怎么办?
下面是我用来切换片段的代码。
private void showFragment(Fragment fragment){
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
if (!fragment.isAdded()) {
fragmentTransaction.replace(R.id.home_frame, fragment).commitAllowingStateLoss();
currentFragment = fragment;
}
}
我的所有片段都扩展了一个基本片段,其中包含以下代码
private static final Field sChildFragmentManagerField;
static {
Field f = null;
try {
f = Fragment.class.getDeclaredField("mChildFragmentManager");
f.setAccessible(true);
} catch (NoSuchFieldException e) {
Log.wtf("LOGTAG", "Error getting mChildFragmentManager field", e);
}
sChildFragmentManagerField = f;
}
@Override
public void onDetach() {
super.onDetach();
if (sChildFragmentManagerField != null) {
try {
sChildFragmentManagerField.set(this, null);
} catch (Exception e) {
Log.e("LOGTAG", "Error setting mChildFragmentManager field", e);
}
}
}
请帮忙。我在商店里遇到了大量的崩溃和糟糕的评论。
下面是我的stacktrace
Fatal Exception: java.lang.IllegalStateException: No activity
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1054)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1049)
at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1871)
at android.app.Fragment.performActivityCreated(Fragment.java:2067)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:912)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:833)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1454)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)