问题
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)
I have seen above a crash on a console for some user.please see more details from here 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);
}
Here,I have update mainActivity code there is viewpager with 4 fragment.
<?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>
Here is 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>
I have also used Appodeal and Admob ads in my application.so initialization of that SDK is also done in this activity.
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'
回答1:
The error is because of conflict between support libraries. You used support.v4 library and support.v7 library both. Please use only one support library then the problem will be fixed.
回答2:
I have debug your error. It will be throw only in one condition as per my debug.
when you are getting null at AppCompatDelegateImplV9.java:489
final ViewGroup windowContentView = (ViewGroup) mWindow.findViewById(android.R.id.content);
so this line never be called AppCompatDelegateImplV9.java:502
contentView.setId(android.R.id.content);
which internally lead to your error. you will get cfl null at AppCompatDelegateImplV9.java:530
ContentFrameLayout cfl = (ContentFrameLayout) mSubDecor.findViewById(android.R.id.content);
and error will thrown at AppCompatDelegateImplV9.java:537
cfl.setDecorPadding(windowDecor.getPaddingLeft(),
windowDecor.getPaddingTop(), windowDecor.getPaddingRight(),
windowDecor.getPaddingBottom());
mWindow is set internally by android at activity init which is object of PhoneWindow
but if any one try to use your application on any other place then mobile means (in virtual machine or running some script to hack it) then it may occur that issue.
So you should check on which device that is occurring and it is also run on google certified os or not.
回答3:
I suspect ContentFrameLayout
is meant for internal use. Looking at its source it doesn't really seem to do anything and the entire class is marked as @hide
which is probably why you get the error (assume you already add the dependency in your apps build gradle). While I'm sure the class has some I internal purpose for the appcompat library. For the app developers, we should probably stick to using regular FrameLayout
.
回答4:
I had the same bug. I think maybe the android:fitsSystemWindows="false"
caused this bug. I have changed it to android:fitsSystemWindows="true"
. It seems improved. You can try it.
来源:https://stackoverflow.com/questions/47842279/application-crash-on-some-devices-with-android-support-v7-widget-contentframelay