赞
踩
本文属于个人平时项目开发过程遇到的一些问题,记录下来并总结解决方案,希望能帮到大家解决问题,有些问题的解决方案是在StackoverFlow上找到的,建议大家遇到问题多去上面找,基本上都能找到解决方案的。
(1)将Eclipse项目导入到Android studio 中 很多点9图出现问题解决方法:
在build.gradle里添加以下两句:
- aaptOptions.cruncherEnabled = false
- aaptOptions.useNewCruncher = false
用来关闭Android Studio的PNG合法性检查的,直接不让它检查。
(2)Android Studio 错误: 非法字符: '\ufeff' 解决方案|错误: 需要class, interface或enum
原因:
Eclipse可以智能的把UTF-8+BOM文件转为普通的UTF-8文件,Android Studio还没有这个功能,所以使用Android Studio编译UTF-8+BOM编码的文件时会出现” 非法字符: '\ufeff' “之类的错误
解决方法:
手动将UTF-8+BOM编码的文件转为普通的UTF-8文件。 用EdItPlus打开.java文件依次:文档》文本编辑》转换文本编码》选择UTF-8编码即可
(3)将项目导入到AS中出现以下问题:
Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. > com.android.bui
- android{
- packagingOptions {
- exclude 'META-INF/DEPENDENCIES.txt'
- exclude 'META-INF/NOTICE'
- exclude 'META-INF/NOTICE.txt'
- exclude 'META-INF/LICENSE'
- exclude 'META-INF/LICENSE.txt'
- }
- }
(4)未知错误
- Error:Timeout waiting to lock cp_proj class cache for build file '/Users/Mr.xiao/Desktop/AndroidShopNC2014MoblieNew/androidShopNC2014Moblie/build.gradle'
- (/Users/Mr.xiao/.gradle/caches/2.10/scripts/build_3cyr7hzjurcc62ge3ixidshos/cp_proj).
- It is currently in use by another Gradle instance.
- Owner PID: unknown
- Our PID: 1412
- Owner Operation: unknown
- Our operation: Initialize cache
- Lock file: /Users/Mr.xiao/.gradle/caches/2.10/scripts/build_3cyr7hzjurcc62ge3ixidshos/cp_proj/cache.properties.lock
(5)修改了Android项目的最小SDK版本之后出现很多stysle文件找不到
- compileSdkVersion 23
- buildToolsVersion "23.0.3"
- defaultConfig {
- applicationId "net.mmloo2014.android"
- minSdkVersion 14
- targetSdkVersion 23
- }
compileSdkVersion 是多少版本的
那么compile 'com.android.support:appcompat-v7:23.2.1’ 就是啥版本的。
(6)Android studio 编译问题:finished with non-zero exit value 2
- Error:Execution failed for task ':androidShopNC2014Moblie:transformClassesWithDexForDebug'.
- >
- com.android.build.api.transform.TransformException:
- com.android.ide.common.process.ProcessException:
- java.util.concurrent.ExecutionException:
- com.android.ide.common.process.ProcessException:
- org.gradle.process.internal.ExecException:
- Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2
- android {
- defaultConfig {
- multiDexEnabled true
- }
- }
(7)Android studio 编译问题:finished with non-zero exit value 1(由于导入的依赖出现重复造成的)
- Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
-
- > com.[Android](http://lib.csdn.net/base/15).build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'F:\Program Files (x86)\[Java](http://lib.csdn.net/base/17)\jdk1.8.0_31\bin\java.exe'' finished with non-zero exit value 1
(8)问题
- Error:Execution failed for task
- ':app:transformClassesWithJarMergingForDebug'.>
- com.android.build.api.transform.TransformException:
- java.util.zip.ZipException:
- duplicate entry: org/apache/http/ConnectionClosedException.class
(9)添加第三方依赖出现的问题
- Error:Execution failed for task ':app:processDebugManifest'.
- >
- Manifest merger failed :
- uses-sdk:minSdkVersion 14 cannot be smaller than version 19 declared in library [com.github.meikoz:basic:2.0.3]
- /AndroidStudioCode/EnjoyLife/app/build/intermediates/exploded-aar/
- com.github.meikoz/basic/2.0.3/AndroidManifest.xml
- Suggestion: use tools:overrideLibrary="com.android.core" to force usage
错误原因
出现这个错误的原因是我引入的第三方库最低支持版本高于我的项目的最低支持版本,异常中的信息显示:我的项目的最低支持版本为14,而第三方库的最低支持版本为19,所以抛出了这个异常。
解决方案
在AndroidManifest.xml文件中标签中添加
<uses-sdk tools:overrideLibrary="xxx.xxx.xxx"/>
其中的xxx.xxx.xxx为第三方库包名,如果存在多个库有此异常,则用逗号分割它们,例如:
<uses-sdk tools:overrideLibrary="xxx.xxx.aaa, xxx.xxx.bbb"/>
这样做是为了项目中的AndroidManifest.xml和第三方库的AndroidManifest.xml合并时可以忽略最低版本限制。
(10)Android studio 编译问题:finished with non-zero exit value 1(由于buildtools版本太高造成的)
- Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
- > com.android.ide.common.process.ProcessException:
- org.gradle.process.internal.ExecException:
- Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
错误原因
buildToolsVersion版本太高,我原来的 buildToolsVersion "24.0.0” 需要jdk1.8,而我的是jdk1.7,所以一直报这个错,刚开始以为是v4包和V7包冲突,因为之前遇到这样的问题,而这次删除V4包之后依然报这个错,上stackoverflow搜了一下,把buildTools版本降下来就好了。
解决方案
- android {
- compileSdkVersion 23
- buildToolsVersion "23.0.3"
- }
(11)Android studio 编译问题:Gradle DSL not found 'android()'
- buildscript {
- repositories {
- jcenter()
- }
-
- dependencies {
- classpath 'com.android.tools.build:gradle:2.1.2'
- }
- }
-
- allprojects {
- repositories {
- jcenter()
- }
- }
- buildscript {
- repositories {
- jcenter()
- }
-
- dependencies {
- classpath 'com.android.tools.build:gradle:2.1.2'
- }
- }
-
-
- allprojects {
- repositories {
- jcenter()
- }
- }
- apply plugin: 'com.android.application'android {
- compileSdkVersion 23
- buildToolsVersion '23.0.3'
- defaultConfig {
- minSdkVersion 9
- targetSdkVersion 23
- versionCode 1
- versionName '1.0'
- }
- }
- dependencies {
- compile 'com.android.support:appcompat-v7:23.2.1'
- }
最后再同步一下sync即可。
(12)Android studio 编译问题:Gradle DSL not found 'android()'
- Error:(51, 52) 错误: -source 1.6 中不支持 diamond 运算符
- (请使用 -source 7 或更高版本以启用 diamond 运算符)
- android {
- compileOptions {
- sourceCompatibility JavaVersion.VERSION_1_7
- targetCompatibility JavaVersion.VERSION_1_7
- }
- }
最后同步一下即可(13)Glide使用问题:使用Glide加载圆角图片,第一次显示占位图
问题描述
最近在项目中使用Glide加载圆形图片,并且设置placehloder和error两个占位图,运行发现,第一次加载图片只显示占位图,需要第二次进入的时候才会正常显示。
如果你刚好使用了这个圆形Imageview库或者其他的一些自定义的圆形Imageview,而你又刚好设置了占位的话,那么,你就会遇到第一个问题。如何解决呢?
方案一
不设置占位图
方案二
使用Glide的Transformation API自定义圆形Bitmap的转换
- /**
- * Glide圆形图片处理
- */
- static class CircleTransform extends BitmapTransformation {
- public CircleTransform(Context context) {
- super(context);
- }
-
- @Override
- protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
- return circleCrop(pool, toTransform);
- }
-
- private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
- if (source == null) return null;
-
- int size = Math.min(source.getWidth(), source.getHeight());
- int x = (source.getWidth() - size) / 2;
- int y = (source.getHeight() - size) / 2;
-
- Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
-
- Bitmap result = pool.get(size, size, Bitmap.Config.RGB_565);
- if (result == null) {
- result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
- }
-
- Canvas canvas = new Canvas(result);
- Paint paint = new Paint();
- paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
- paint.setAntiAlias(true);
- float r = size / 2f;
- canvas.drawCircle(r, r, r, paint);
- return result;
- }
-
- @Override
- public String getId() {
- return getClass().getName();
- }
- }
使用方法:
Glide.with(context).load(imageUrl).placeholder(placeholder).error(errorImage).transform(new CircleTransform(context)).into(imageView);
方案三
重写Glide的图片加载监听方法,具体如下:
- Glide.with(mContext)
- .load(url)
- .placeholder(R.drawable.loading_drawable)
- .into(new SimpleTarget<Bitmap>(width, height) {
- @Override public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
- // setImageBitmap(bitmap) on CircleImageView
- }
- });
注意事项:
该方法在listview上复用有问题的bug,如果在listview中加载CircleImageView,请不要使用该方法。
方案四:不使用Glide的默认动画:
- Glide.with(mContext)
- .load(url)
- .dontAnimate()
- .placeholder(R.drawable.loading_drawable)
- .into(circleImageview);
(14)json数据解析问题:json串头部出现字符:"\ufeff" 解决方法
异常信息
org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
解析服务器返回 的json格式数据时,我们可能会发现,数据格式上是没有问题的,但是仔细对比会发现,在json串头部发现字符:"\ufeff"
客户端解决方案:
- /**
- * 异常信息:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
- * json串头部出现字符:"\ufeff" 解决方法
- * @param data
- * @return
- */
- public static final String removeBOM(String data) {
- if (TextUtils.isEmpty(data)) {
- return data;
- }
- if (data.startsWith("\ufeff")) {
- return data.substring(1);
- }
- else {
- return data;
- }
- }
服务器端解决方案:
将输出此json的php源码重新用editplus之类用utf-8无BOM的编码保存。不要用windows系统自带的记事本编辑php源码,这个BOM就是记事本这些windows自带的编辑器引入的。
(15)Android studio编译问题:not found ndk()
问题
Error:(15, 0) Gradle DSL method not found: 'ndk()' method-not-found-ndk
解决方案
出现该问题,可能是由于ndk配置在build.gradle配置文件中位置弄错导致的
- apply plugin: 'com.android.application'
- android {
- compileSdkVersion 23
- buildToolsVersion "23.0.2"
-
- defaultConfig {
- applicationId "com.guitarv.www.ndktest"
- minSdkVersion 17
- targetSdkVersion 23
- versionCode 1
- versionName "1.0"
- ndk {
- moduleName = "HelloJNI"
- }
- sourceSets.main {
- jni.srcDirs = []
- jniLibs.srcDir "src/main/libs"
- }
- }
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- }
- }
(16)Android studio导入其他的项目:UnsupportedMethodException
问题
- UnsupportedMethodException
- Unsupported method: AndroidProject.getPluginGeneration().
- The version of Gradle you connect to does not support that method.
- To resolve the problem you can change/upgrade the target version of Gradle you connect to.
- Alternatively, you can ignore this exception and read other information from the model.
解决方案
将根目录中的build.gradle文件中的gradle版本号,出现错误之前,我的是1.3.0,修改成2.2.0之后重新编译一下就可以运行了。
- dependencies {
- classpath 'com.android.tools.build:gradle:1.3.0'
- }
将这个版本号改成你其他项目能够运行成功的版本号即可
(17)Android studio更新到2.1.1之后使用CollapsingToolbarLayout出现Error inflating class CollapsingToolbarLayout
之前在项目中使用了CollapsingToolbarLayout,效果还是可以的,但是Android stuido更新到2.1.1版本之后出现Error inflating class CollapsingToolbarLayout 异常崩溃
异常信息如下所示:
- com.test.android/com.test.android.ui.activity.RandomActivity}: android.view.InflateException: Binary XML file line #22: Error inflating class android.support.design.widget.CollapsingToolbarLayout
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
- at android.app.ActivityThread.access$800(ActivityThread.java:151)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
- at android.os.Handler.dispatchMessage(Handler.java:102)
- at android.os.Looper.loop(Looper.java:135)
- at android.app.ActivityThread.main(ActivityThread.java:5254)
- at java.lang.reflect.Method.invoke(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:372)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
- Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class android.support.design.widget.CollapsingToolbarLayout
- at android.view.LayoutInflater.createView(LayoutInflater.java:633)
- at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
- at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
- at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
- at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
- at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
- at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
- at com.test.android.ui.activity.RefreshableActivity.onCreate(RefreshableActivity.java:31)
- at android.app.Activity.performCreate(Activity.java:5990)
- at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
- at android.app.ActivityThread.access$800(ActivityThread.java:151)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
- at android.os.Handler.dispatchMessage(Handler.java:102)
- at android.os.Looper.loop(Looper.java:135)
- at android.app.ActivityThread.main(ActivityThread.java:5254)
- at java.lang.reflect.Method.invoke(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:372)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
-
- Caused by: java.lang.NoSuchMethodError: No static method setLayoutDirection(Landroid/graphics/drawable/Drawable;I)V in class Landroid/support/v4/graphics/drawable/DrawableCompat; or its super classes (declaration of 'android.support.v4.graphics.drawable.DrawableCompat' appears in /data/app/com.test.android-1/base.apk)
- at android.support.design.widget.CollapsingToolbarLayout.setStatusBarScrim(CollapsingToolbarLayout.java:663)
- at android.support.design.widget.CollapsingToolbarLayout.<init>(CollapsingToolbarLayout.java:197)
- at android.support.design.widget.CollapsingToolbarLayout.<init>(CollapsingToolbarLayout.java:132)
- at java.lang.reflect.Constructor.newInstance(Native Method)
- at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
- at android.view.LayoutInflater.createView(LayoutInflater.java:607)
- at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
- at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
- at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
- at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
- at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
- at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
- at com.test.android.ui.activity.RefreshableActivity.onCreate(RefreshableActivity.java:31)
- at android.app.Activity.performCreate(Activity.java:5990)
- at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
- at android.app.ActivityThread.access$800(ActivityThread.java:151)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
- at android.os.Handler.dispatchMessage(Handler.java:102)
- at android.os.Looper.loop(Looper.java:135)
- at android.app.ActivityThread.main(ActivityThread.java:5254)
- at java.lang.reflect.Method.invoke(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:372)
解决方案
在项目的build.gradle文件中添加下面一行,同步一下即可
- compile ('com.android.support:support-v4:23.4.0'){
- force = true;
- }
(18)Android studio gradle编译异常
java.lang.UnsupportedClassVersionError: com/android/build/gradle/AppPlugin : Unsupported major.minor version 52.0
很显然是class版本不支持。经查询,Android Studio2.2必须使用JDK8及以上版本,而且是强制的。
所以呢,赶紧下了个JDK8最新版的。安装完毕,把JAVA_HOME指向了JDK8,实测JDK7和8是可以共存的。
那么,重启Android Studio后问题解决,Build Successful !
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。