getBaseActivity()。onBackPressed()上的应用崩溃

时间:2018-10-11 07:14:08

标签: android android-fragments crash onbackpressed targetsdkversion

我最近通过以下配置升级了Android应用-

compileSdkVersion 27
buildToolsVersion "25.0.3"

defaultConfig {
    multiDexEnabled true
    applicationId "com.test.www"
    versionCode 11
    versionName "2.1"
    minSdkVersion 23
    targetSdkVersion 27
}

compile('com.android.support:appcompat-v7:27.0.1') {
    /*    compile('com.android.support:appcompat-v7:27.0.2') {*/
    exclude module: 'support-v4'
}
compile('com.android.support:design:27.0.1') {
    /*  compile('com.android.support:design:27.0.2') {*/
    exclude module: 'support-v4'
}
compile('com.android.support:support-v4:27.0.1') {
    /*    compile('com.android.support:support-v4:27.0.2') {*/
    exclude module: 'support-v4'
}
compile('com.android.support:recyclerview-v7:27.0.1') {
    /*    compile('com.android.support:recyclerview-v7:27.0.2') {*/
    exclude module: 'support-v4'
}       

之后- 当我按下主页或后退按钮并出现以下错误时,我的应用程序崩溃-

  

代码-getBaseActivity()。onBackPressed(); (父片段返回   基本活动)

     

java.lang.IllegalStateException:指定的子对象已经有一个   父母您必须先在孩子的父母上调用removeView()。

为了解决这个问题,我在基本片段中添加了以下代码- 其他类正在扩展-

  @Override
public void onDestroyView() {
    super.onDestroyView();
    if (getActivity() != null) {
        ViewGroup parentViewGroup = (ViewGroup) this.getView().getParent();
        if (parentViewGroup != null) {
            parentViewGroup.removeAllViews();
        }
    }
}

这样做之后,我的当机问题已经解决,但是我在recyclerview中使用片段的位置在1秒内变空,第一次单击时,我看到记录是从后端加载的,但由于上述代码,它立即在1秒内消失了。

在使用上述配置升级应用程序之前,我没有遇到过崩溃问题。

请让我知道解决崩溃问题的正确方法,或者通过添加以上代码后片段列表自动消失的方法来帮助我解决问题。

关于, Hathora

0 个答案:

没有答案
相关问题