第一个问题
clasdef 4.4 明明多个dex都设置正常,包括依赖引用 multiDexEnabled true
implementation 'com.android.support:multidex:1.0.1'
并在appcontext里面加上
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
并且
<application
android:name=".AppContext"
依然没有错误,太让我怀疑人生了,于是我打包看了看就一个dex,好吧,我全部删掉,关闭 多个dex好了,ok完美,这个问题是逃避的解决方法,因为其它重新编译的方法都尝试过了。
第二个问题
横屏切换 测试内存不足界面被销毁回来之后崩溃,调用onRestoreInstanceState
和onRestoreInstanceState`
结果打死都不调用onRestoreInstanceState
只调用前者,这个问题就算了,不管了,那么内存不足引发的崩溃问题复现了,
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2687)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2785)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4603)
at android.app.ActivityThread.-wrap19(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1540)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6342)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:107)
java.lang.IllegalArgumentException: No view found for id 0x7f09009f (cn.qssq666.robot:id/fragment_space_x) for fragment Tab1PosterMenuContainFragment{df83d73 #10 id=0x7f09009f}
网上的方法无非就是使用getChildFragmenManager,我使用childFragmentManager 页面是空白的,我头脑发胀,反复的 调试parent fragment的替换方式为getChildManager或者fragmetn的 fragmentManager或者getActivity().getSuppoertFragmentManager()
都不能解决问题,到底应该怎么做,
在我写这篇文章的时候我突然焕然大悟,空白是不是因为 fragment被窝做了某些懒加载等导致的。
final ArrayList<Class<? extends Fragment>> listFragment = new ArrayList();
listFragment.add(Tab1PosterMenuContainFragment.class);
listFragment.add(Tab1PosterMenuContainInvitationFragment.class);
Class<? extends Fragment> fragmentClass = listFragment.get(index);
try{
FragmentUtil.replaceFragment(getActivity(), fragmentClass, R.id.fragment_space_x, false);//todo 空白问题没法解决了。 但是onSaveInstanceState No view found for id for...
}catch (IllegalArgumentException e){
final ArrayList<BaseLazyLoadFragment> list = new ArrayList();
list.add(new Tab1PosterMenuContainFragment());
list.add(new Tab1PosterMenuContainInvitationFragment());
list.add(new Tab1PosterMenuContainVideoFragment());
BaseLazyLoadFragment fragment = list.get(index);
fragment.setLazyLoadMode(false);
FragmentUtil.replaceChildFragment(PosterFragment.this,getActivity(), fragment,R.id.fragment_space_x,false);
}
那么我就这样写,或者直接使用catch里面的代码应该就可以了。
由于fragment一个嵌套一个嵌套了很多个了,
那么问题太多了。
java.lang.IllegalStateException: Fragment has not been attached yet.
at android.support.v4.app.Fragment.instantiateChildFragmentManager(Fragment.java:2308)
包括vivewpager里面使用getFragmentManger导致滑动不流畅之类的。
改良之后的fragment替换代码
public static <T> void replaceFragment(final Fragment parentFragment, final FragmentActivity activity, final Fragment fragment, final Bundle bundle,
final int containerId, final String tag, final String backStackName,
final boolean addToBackStack, final boolean needAnim) {
Bundle tempBundle = bundle;
if (tempBundle != null) {
fragment.setArguments(tempBundle);
}
FragmentManager supportFragmentManager;
if (parentFragment != null) {
if (!parentFragment.isAdded()) {
Log.e(TAG, "fragment not added");
return;
}
if (parentFragment.getActivity() == null) {
Log.e(TAG, " java.lang.IllegalStateException: Fragment has not been attached yet.");//继续调用也没用 还是会崩溃
return;
}
supportFragmentManager = parentFragment.getChildFragmentManager();
} else {
supportFragmentManager = activity.getSupportFragmentManager();
if (fragment instanceof FragmentOnStartNeedCheckVisible) {
((FragmentOnStartNeedCheckVisible) fragment).setOnStartNeedCheckData(true);
}
}
if (fragment != null && supportFragmentManager != null) {
FragmentTransaction transaction = supportFragmentManager.beginTransaction();
if (needAnim) {
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (activity == null || activity.isDestroyed()) {
Log.e(TAG, "activity is destory! " + Log.getStackTraceString(new Throwable()));
return;
}
}
// transaction.setCustomAnimations(R.animator.fragment_enter_left, R.animator.fragment_exit_left);
Log.i(TAG, "containerId:" + (containerId == CONTAINER_ID_DEFAULT ? R.id.fragment_space : containerId) + "");
transaction.replace(containerId == CONTAINER_ID_DEFAULT ? R.id.fragment_space : containerId, fragment, tag);
if (addToBackStack) {
transaction.addToBackStack(backStackName);
}
transaction.commitAllowingStateLoss();
/*
Can not perform this action after onSaveInstanceState
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1493)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
*/
} else {
Log.e(TAG, "fragmentManager is Empty! " + Log.getStackTraceString(new Throwable()));
}
// }
// }, 50);
}