Android Studio project works on Lollipop but does

2019-02-19 15:11发布

问题:

I have an Android Studio project that is working fine on devices with Android build version Lollipop but is throwing an exception when trying to run it on a device with android build version Kitkat. This is my project's build.gradle file:

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

            renderscriptTargetApi 22
            renderscriptSupportModeEnabled true

    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile files('libs/org.apache.commons.io.jar')
    compile files('libs/commons-codec-1.10.jar')
    compile('com.twitter.sdk.android:twitter:1.6.0@aar') {
        transitive = true;
    }
    compile files('libs/pinit-sdk-1.0.jar')
    compile project(':bounceScroller')
    compile 'com.ogaclejapan.smarttablayout:library:1.2.1@aar'
    compile 'com.facebook.android:facebook-android-sdk:4.1.0'
    compile 'com.android.support:appcompat-v7:21.2.0'
    compile 'com.github.navasmdc:MaterialDesign:1.5@aar'
    compile 'com.nineoldandroids:library:2.4.+'
    compile 'com.esri.arcgis.android:arcgis-android:10.2.6-2'
    compile 'com.github.clans:fab:1.5.3'
    compile 'com.github.pedrovgs:draggablepanel:1.8'
    compile 'com.android.support:support-v4:21.2.0'
    compile 'com.ogaclejapan.smarttablayout:utils-v4:1.2.1@aar'
    compile 'com.github.bumptech.glide:glide:3.6.0'
    compile project(':rangeseekbar')
    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.android.support:cardview-v7:22.2.0'
    compile project(':blurry')
    compile 'com.google.android.gms:play-services:7.8.0'
}

and here is the error I am getting when I run the application on a Kitkat device:

java.lang.RuntimeException: Unable to start activity: android.view.InflateException: Binary XML file line #14: Error inflating class android.support.v4.view.ViewPager
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
            at android.app.ActivityThread.access$900(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class android.support.v4.view.ViewPager
            at android.view.LayoutInflater.createView(LayoutInflater.java:626)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:359)
            at android.app.Activity.setContentView(Activity.java:2010)
            at android.visitabudhabi.com.vad.Walkthrough.onCreate(Walkthrough.java:32)
            at android.app.Activity.performCreate(Activity.java:5426)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
            at android.app.ActivityThread.access$900(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:600)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:359)
            at android.app.Activity.setContentView(Activity.java:2010)
            at android.visitabudhabi.com.vad.Walkthrough.onCreate(Walkthrough.java:32)
            at android.app.Activity.performCreate(Activity.java:5426)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
            at android.app.ActivityThread.access$900(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoClassDefFoundError: android.support.v4.view.ViewConfigurationCompatFroyo
            at android.support.v4.view.ViewConfigurationCompat$FroyoViewConfigurationVersionImpl.getScaledPagingTouchSlop(ViewConfigurationCompat.java:56)
            at android.support.v4.view.ViewConfigurationCompat.getScaledPagingTouchSlop(ViewConfigurationCompat.java:105)
            at android.support.v4.view.ViewPager.initViewPager(ViewPager.java:368)
            at android.support.v4.view.ViewPager.<init>(ViewPager.java:356)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:600)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:359)
            at android.app.Activity.setContentView(Activity.java:2010)
            at android.visitabudhabi.com.vad.Walkthrough.onCreate(Walkthrough.java:32)
            at android.app.Activity.performCreate(Activity.java:5426)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
            at android.app.ActivityThread.access$900(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

This is my XML that has the ViewPager:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:overScrollMode="never" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:layout_below="@+id/pager"
        android:layout_gravity="bottom"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/first"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3.3"
            android:background="#c7333f" />

        <ImageView
            android:id="@+id/second"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3.3"
            android:background="#FFF" />

        <ImageView
            android:id="@+id/third"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3.3"
            android:background="#fff" />
    </LinearLayout>


</FrameLayout>

Anybody knows how to solve this?

回答1:

This is how I solved the problem if anyone gets here.

After I searched deeply about the problem, I found out that it's not an inflating exception though that's what the logcat said, the real exception was OutOfMemory exception caused by the infamous 65K problem click here for more information about the problem and it was wrapped with the inflate excpetion.

To solve the problem, I firstly added the multidex support library in my build.gradle file:

compile 'com.android.support:multidex:1.0.0'

Then I followed these three simple steps to make it work:

  1. If you’re not implementing an Application class yourself, you can simply define the library’s MultiDexApplication in your Android Manifest file under the application tag:

    android:name="android.support.multidex.MultiDexApplication"

  2. If you are implementing your own Application class, you can either simply override the MultiDexApplication file:

    public class MyAwesomeApplication extends MultiDexApplication {

  3. Or if your application class is already extending another class, you can just override attachBaseContext method and add the following call to it:

    @Override protected void attachBaseContext(Context base) { super.attachBaseContext(context); Multidex.install(this); }

I already had my own application class that extends from class "Application" so I jumped directly to step 3 and voila it worked on Kitkat and on Lollipop.

Hope this helps anyone. Happy Coding.



回答2:

The main issue is that your compileSdkVersion does not match major version of support libraries you include with the project. Follow these rules:

Set targetSdkVersion to 22 because you want to use Lollipop features on Lollipop devices (and don't care about Marshmallow for now).

Set compileSdkVersion to 23 because it's the newest.

Set buildToolsVersion to "23.0.1" because it's the newest.

Set support library versions to the latest matching compileSdkVersion:

compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:support-v4:23.1.0'
compile 'com.android.support:recyclerview-v7:23.1.0'
compile 'com.android.support:cardview-v7:23.1.0'


回答3:

Okay . At first call fill_parent .

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:overScrollMode="never" />

And use this instead of yours .

compile 'com.android.support:support-v4:21.0.3'

set targetSdkVersion 19

Edit1

you set

compileSdkVersion 23
buildToolsVersion "23.0.1"

and

targetSdkVersion 23

I hope it will helps you .