Android Studio project works on Lollipop but does not work on Kitkat(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>
有人知道怎么解决吗?
推荐答案
如果有人来了,我就是这样解决问题的.
This is how I solved the problem if anyone gets here.
在深入研究了这个问题之后,我发现这不是一个膨胀的异常,虽然 logcat 是这么说的,真正的异常是臭名昭著的 65K 问题导致的 OutOfMemory 异常 单击此处了解有关该问题的更多信息,它已被 inflate excpetion 包装.
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:
如果你自己没有实现 Application 类,你可以简单地在你的 Android Manifest 文件中的 application 标签下定义库的 MultiDexApplication:
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:
@覆盖受保护的无效 attachBaseContext(上下文基础){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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android Studio 项目适用于 Lollipop,但不适用于 Kitkat
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01