Android Studio项目适用于Lollipop,但不适用于Kitkat [英] Android Studio project works on Lollipop but does not work on Kitkat

查看:96
本文介绍了Android Studio项目适用于Lollipop,但不适用于Kitkat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android Studio项目,该项目在具有Android构建版本Lollipop的设备上可以正常运行,但是在尝试在具有Android构建版本Kitkat的设备上运行该项目时会引发异常.这是我项目的build.gradle文件:

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'
}

这是我在Kitkat设备上运行应用程序时遇到的错误:

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)

这是我的具有ViewPager的XML:

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?

推荐答案

如果有人到达这里,这就是我解决问题的方式.

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

在深入研究问题之后,我发现这不是膨胀的异常,尽管这是logcat所说的,真正的异常是由臭名昭著的65K问题引起的OutOfMemory异常. android.com/tools/building/multidex.html"rel =" noreferrer>单击此处以获取有关该问题的更多信息,并且它被充气的包裹物包裹着.

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.

为解决此问题,我首先在我的build.gradle文件中添加了multidex支持库:

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. 如果您自己没有实现Application类,则可以在Android Manifest文件中的application标签下简单地定义库的MultiDexApplication:

  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""

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

如果要实现自己的Application类,则可以简单地重写MultiDexApplication文件:

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

公共类MyAwesomeApplication扩展了MultiDexApplication {

public class MyAwesomeApplication extends MultiDexApplication {

或者,如果您的应用程序类已经在扩展另一个类,则可以重写attachBaseContext方法并向其中添加以下调用:

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

@Override 受保护的void attachBaseContext(Context base){ super.attachBaseContext(context); Multidex.install(this); }

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

我已经有自己的应用程序类,该类从"Application"类扩展为所以我直接跳到第3步,瞧瞧它在Kitkat和Lollipop上都起作用.

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.

这篇关于Android Studio项目适用于Lollipop,但不适用于Kitkat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆