【问题标题】:Application crash on some devices with android.support.v7.widget.ContentFrameLayout.setDecorPadding使用 android.support.v7.widget.ContentFrameLayout.setDecorPadding 在某些设备上应用程序崩溃
【发布时间】:2017-12-16 03:33:10
【问题描述】:
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage/com.mypackage.activities.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.ContentFrameLayout.setDecorPadding(int, int, int, int)' on a null object reference
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2721)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1509)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6286)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.ContentFrameLayout.setDecorPadding(int, int, int, int)' on a null object reference
       at android.support.v7.app.AppCompatDelegateImplV9.applyFixedSizeWindow(AppCompatDelegateImplV9.java:537)
       at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:331)
       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
       at com.watchlivetv.onlineradioapp.activities.HomeActivity.onCreate(HomeActivity.java:99)
       at android.app.Activity.performCreate(Activity.java:6778)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1121)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2675)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1509)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6286)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我在上面看到了一些用户在控制台上的崩溃。请从这里查看更多详细信息http://crashes.to/s/c98e1c603ef

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.mainToolBar);
        setSupportActionBar(toolbar);

        actionBar = toolbar.findViewById(R.id.zalunuActionBar);
        viewPager = findViewById(R.id.pager);
        actionBar.setTabListener(this);
        viewPager.setAdapter(new MainPagerAdapter(getFragmentManager()));
        viewPager.setOffscreenPageLimit(viewPager.getChildCount());
        viewPager.addOnPageChangeListener(this);

    }

在这里,我更新了 mainActivity 代码,有 4 个片段的 viewpager。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false"
    tools:openDrawer="end">

    <include layout="@layout/activity_main_content" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fitsSystemWindows="true">

        <include layout="@layout/drawer" />

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

这里是 R.layout.activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/mainToolBar"
            style="@style/toolbar"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp">

            <include layout="@layout/activity_main_custom_action_bar" />

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>


    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/footerLayout"
        android:layout_below="@+id/appBarLayout" />


    <LinearLayout
        android:id="@+id/footerLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/bannerLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


    </LinearLayout>

</RelativeLayout>

我还在我的应用程序中使用了 Appodeal 和 Admob 广告。因此该 SDK 的初始化也在此活动中完成。

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
    maven {
        url "https://maven.google.com"
    }
}

android {

    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "projectId"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 41
        versionName "5.0.5"
        multiDexEnabled true
        ndk {
            abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
        }
        signingConfig signingConfigs.config
        manifestPlaceholders = [onesignal_app_id               : OneSignalID,
                                onesignal_google_project_number: "REMOTE"]
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            lintOptions {
                disable 'MissingTranslation'
            }
            signingConfig signingConfigs.config
        }
        debug {
            signingConfig signingConfigs.config
            minifyEnabled false
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
    productFlavors {
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

}

crashlytics {
    enableNdk true
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation files('libs/org.apache.http.legacy.jar')
    implementation files('libs/twitter4j-core-4.0.2.jar')
    implementation project(':appiraterandroidmaster')
    implementation project(path: ':vitamio')
    implementation files('libs/appodeal-2.1.7.jar')
    implementation files('libs/YouTubeAndroidPlayerApi.jar')
    implementation files('libs/jdom2-2.0.6.jar')
    implementation('com.crashlytics.sdk.android:crashlytics:2.8.0@aar') {
        transitive = true
    }
    implementation('com.crashlytics.sdk.android:crashlytics-ndk:2.0.0@aar') {
        transitive = true
    }
    implementation 'com.android.volley:volley:1.0.0'
    implementation 'com.android.support:support-v13:26.1.0'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    implementation 'com.android.support:cardview-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.google.android.gms:play-services-ads:11.8.0'
    implementation 'com.google.android.gms:play-services-location:11.8.0'
    implementation 'com.google.android.gms:play-services-gcm:11.8.0'
    implementation 'com.google.firebase:firebase-appindexing:11.8.0'
    implementation 'com.google.firebase:firebase-core:11.8.0'
    implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'
    implementation 'com.facebook.android:facebook-android-sdk:4.27.0'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.onesignal:OneSignal:3.6.5'
    implementation 'com.github.bumptech.glide:glide:4.3.1'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.github.rubensousa:gravitysnaphelper:1.3'
    implementation 'com.android.support:multidex:1.0.2'
    implementation 'com.github.clans:fab:1.6.4'
    /*admod mediation*/
    implementation 'com.facebook.android:audience-network-sdk:4.26.1'
    implementation 'com.google.ads.mediation:facebook:4.26.1.0'
    implementation 'com.applovin:applovin-sdk:7.6.0'
    implementation 'com.google.ads.mediation:applovin:7.6.0.0'
    implementation('com.vungle:publisher-sdk-android:5.3.0@aar') {
        transitive = true
    }
    implementation 'com.google.ads.mediation:vungle:5.3.0.0'
    implementation 'com.inmobi.monetization:inmobi-ads:7.0.0'
    implementation 'com.google.ads.mediation:inmobi:6.2.4.0'
    implementation 'com.adcolony:sdk:3.2.1'
    implementation 'com.android.support:support-annotations:27.0.2'
    implementation 'com.google.ads.mediation:adcolony:3.2.1.1'
    //    admob mediation end
    testImplementation 'junit:junit:4.12'
    implementation files('libs/OneAudience-sdk-release-6.0.0.0.jar')
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

【问题讨论】:

  • 可能是 sdk 和应用兼容库不匹配。您的应用兼容库的版本是什么?
  • @Henry appcompat 有版本26.1.0
  • 把你的java代码放在这里,出错的地方。
  • @GunduBandagar 只有一个 ViewPager 你可以从这里查看详细信息crashes.to/s/c98e1c603ef
  • 我看到了,但我想看看你的 HomeActivity onCreate 代码,那里出错了。

标签: android android-appcompat android-framelayout androiddesignsupport


【解决方案1】:

错误是由于支持库之间的冲突。 您同时使用了 support.v4 库和 support.v7 库。请仅使用一个支持库,然后问题将得到解决。

【讨论】:

  • 我使用 gradlew -q dependencies app:dependencies --configuration debugAndroidTestCompileClasspath 进行 degub 没有冲突,我在代码中只有 com.android.support:appcompat-v7:。和com.android.support:support-v4: 也更新 gradle.build 文件
  • 我面临同样的问题并且没有依赖冲突。还有其他解决办法吗?
【解决方案2】:

我已调试您的错误。根据我的调试,它只会在一种情况下抛出。

当您在 AppCompatDelegateImplV9.java:489 获得 null 时

final ViewGroup windowContentView = (ViewGroup) mWindow.findViewById(android.R.id.content);

所以这条线永远不会被称为 AppCompatDelegateImplV9.java:502

contentView.setId(android.R.id.content);

这会在内部导致您的错误。您将在 AppCompatDelegateImplV9.java:530 处获得 cfl null

ContentFrameLayout cfl = (ContentFrameLayout) mSubDecor.findViewById(android.R.id.content);

AppCompatDelegateImplV9.java:537 会抛出错误

cfl.setDecorPadding(windowDecor.getPaddingLeft(),
            windowDecor.getPaddingTop(), windowDecor.getPaddingRight(),
            windowDecor.getPaddingBottom());

mWindow 由 android 在 Activity init 内部设置,它是 PhoneWindow 的对象

但如果有人试图在任何其他地方使用您的应用程序,那么移动设备意味着(在虚拟机中或运行一些脚本来破解它),那么它可能会出现这个问题。

因此,您应该检查正在发生的设备以及它是否也在谷歌认证的操作系统上运行。

【讨论】:

  • crashes.to/s/c98e1c603ef 这里所有细节都是提到导致问题的设备,我该如何解决这个错误?根据日志主要错误在chrombook
  • @OmInfowaveDevelopers 我无法在崩溃报告中看到更多详细信息,因为我不是您的 Fabric 团队成员,我无权查看该内容
  • 90% 的崩溃报告在 Asus Chromebook Flip C100PA android 版本 7
【解决方案3】:

我怀疑ContentFrameLayout 是供内部使用的。 Looking at its source 它似乎并没有真正做任何事情,整个类被标记为 @hide 这可能是您收到错误的原因(假设您已经在应用程序构建 gradle 中添加了依赖项)。虽然我确信该类对 appcompat 库有一些内部用途。对于应用程序开发人员,我们可能应该坚持使用常规的FrameLayout

【讨论】:

  • 有什么解决方案?我们可以为解决方案做些什么吗?
  • 只需将 ContentFrameLayout 替换为 FrameLayout。 ContentFrameLayout 只是 FrameLayout 的包装类
  • 我没用过ContentFrameLayout,可能在我用过的库里。如何识别它的使用位置?如果它来自某个库,如何替换它。
【解决方案4】:

我有同样的错误。我想也许android:fitsSystemWindows="false" 导致了这个错误。我已将其更改为android:fitsSystemWindows="true"。似乎有所改善。你可以试试看。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多