Android studio我們那些年遇到的坑(二)

Android studio我們那些年遇到的坑(二)

本文屬于個(gè)人平時(shí)項(xiàng)目開(kāi)發(fā)過(guò)程遇到的一些問(wèn)題,記錄下來(lái)并總結(jié)解決方案,希望能幫到大家解決問(wèn)題,有些問(wèn)題的解決方案是在StackoverFlow上找到的,建議大家遇到問(wèn)題多去上面找,基本上都能找到解決方案的。

(1)將Eclipse項(xiàng)目導(dǎo)入到Android studio 中 很多點(diǎn)9圖出現(xiàn)問(wèn)題解決方法:

在build.gradle里添加以下兩句:

aaptOptions.cruncherEnabled = false? ?

aaptOptions.useNewCruncher = false

用來(lái)關(guān)閉Android Studio的PNG合法性檢查的,直接不讓它檢查。

(2)Android Studio 錯(cuò)誤: 非法字符: '\ufeff' 解決方案|錯(cuò)誤: 需要class, interface或enum

原因:

Eclipse可以智能的把UTF-8+BOM文件轉(zhuǎn)為普通的UTF-8文件,Android Studio還沒(méi)有這個(gè)功能,所以使用Android Studio編譯UTF-8+BOM編碼的文件時(shí)會(huì)出現(xiàn)”非法字符: '\ufeff' “之類的錯(cuò)誤

解決方法:

手動(dòng)將UTF-8+BOM編碼的文件轉(zhuǎn)為普通的UTF-8文件。**

**用EdItPlus打開(kāi).java文件依次:文檔》文本編輯》轉(zhuǎn)換文本編碼》選擇UTF-8編碼即可

(3)將項(xiàng)目導(dǎo)入到AS中出現(xiàn)以下問(wèn)題:

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. > com.android.bui

解決方法:

在build.grade中添加以下代碼:

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)未知錯(cuò)誤

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

解決方案

以上是錯(cuò)誤提示。

解決的思路很簡(jiǎn)單只需要把cache.properties.lock文件刪除了就可以了。當(dāng)時(shí)我們刪除的時(shí)候會(huì)被占用這時(shí)候需要進(jìn)入任務(wù)管理器結(jié)束關(guān)于java的進(jìn)程就行比如 java 的jdk 刪除后重啟讓java jdk啟動(dòng)? 啟動(dòng)Android Studio就能啟動(dòng)APK了。

(5)修改了Android項(xiàng)目的最小SDK版本之后出現(xiàn)很多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 編譯問(wèn)題:finished with non-zero exit value 2

問(wèn)題:

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

解決方案

這個(gè)錯(cuò)誤在app的build.gradle里面添加下面這句就好了。

android {

defaultConfig {

multiDexEnabled true

? ? ? }

}

(7)Android studio 編譯問(wèn)題:finished with non-zero exit value 1(由于導(dǎo)入的依賴出現(xiàn)重復(fù)造成的)

問(wèn)題:

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

解決方案

這個(gè)是因?yàn)橐蕾嚢貜?fù)了 (像v4和nineoldandroids),app中實(shí)現(xiàn)了對(duì)easeUI的依賴,但是app和easeUI都添加了對(duì)這個(gè)包的依賴。所以就報(bào)這個(gè)錯(cuò)誤,修改之后再報(bào),就clean,rebuild一下。

(8)問(wèn)題

Error:Execution failed for task

':app:transformClassesWithJarMergingForDebug'.>

com.android.build.api.transform.TransformException:

java.util.zip.ZipException:

duplicate entry: org/apache/http/ConnectionClosedException.class

解決方案

這個(gè)是在我們啟動(dòng)的時(shí)候報(bào)錯(cuò)的,而不是在編譯的時(shí)候,原因是這樣的,報(bào)這個(gè)錯(cuò)是因?yàn)橛?個(gè)庫(kù)中存在相同的類。大家可以看到stackoverflow上有人也提了這樣的問(wèn)題。只需要?jiǎng)h除其中的一個(gè)就可以解決了。

(9)添加第三方依賴出現(xiàn)的問(wèn)題

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

錯(cuò)誤原因

出現(xiàn)這個(gè)錯(cuò)誤的原因是我引入的第三方庫(kù)最低支持版本高于我的項(xiàng)目的最低支持版本,異常中的信息顯示:我的項(xiàng)目的最低支持版本為14,而第三方庫(kù)的最低支持版本為19,所以拋出了這個(gè)異常。

解決方案

在AndroidManifest.xml文件中標(biāo)簽中添加

<uses-sdk tools:overrideLibrary="xxx.xxx.xxx"/>

其中的xxx.xxx.xxx為第三方庫(kù)包名,如果存在多個(gè)庫(kù)有此異常,則用逗號(hào)分割它們,例如:

<uses-sdk tools:overrideLibrary="xxx.xxx.aaa, xxx.xxx.bbb"/>

這樣做是為了項(xiàng)目中的AndroidManifest.xml和第三方庫(kù)的AndroidManifest.xml合并時(shí)可以忽略最低版本限制。

(10)Android studio 編譯問(wèn)題:finished with non-zero exit value 1(由于buildtools版本太高造成的)

錯(cuò)誤

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

錯(cuò)誤原因

buildToolsVersion版本太高,我原來(lái)的 buildToolsVersion "24.0.0” 需要jdk1.8,而我的是jdk1.7,所以一直報(bào)這個(gè)錯(cuò),剛開(kāi)始以為是v4包和V7包沖突,因?yàn)橹坝龅竭@樣的問(wèn)題,而這次刪除V4包之后依然報(bào)這個(gè)錯(cuò),上stackoverflow搜了一下,把buildTools版本降下來(lái)就好了。

解決方案

android {? ?

compileSdkVersion 23? ?

buildToolsVersion "23.0.3"?

}

(11)Android studio 編譯問(wèn)題:Gradle DSL not found 'android()'

問(wèn)題


clipboard.png

解決方案

配置build.gradle:

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()

? ? }

}

配置app/build.gradle:

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 編譯問(wèn)題:Gradle DSL not found 'android()'

問(wèn)題描述

Error:(51, 52) 錯(cuò)誤: -source 1.6 中不支持 diamond 運(yùn)算符

(請(qǐng)使用 -source 7 或更高版本以啟用 diamond 運(yùn)算符)

解決方案

方案一


將標(biāo)紅處設(shè)置為1.7.png


修改soure為1.7.png

方案二

在build gradle中進(jìn)行配置如下代碼:

android {

compileOptions {

sourceCompatibility JavaVersion.VERSION_1_7

targetCompatibility JavaVersion.VERSION_1_7

}

}

最后同步一下即可

(13)Glide使用問(wèn)題:使用Glide加載圓角圖片,第一次顯示占位圖

問(wèn)題描述

最近在項(xiàng)目中使用Glide加載圓形圖片,并且設(shè)置placehloder和error兩個(gè)占位圖,運(yùn)行發(fā)現(xiàn),第一次加載圖片只顯示占位圖,需要第二次進(jìn)入的時(shí)候才會(huì)正常顯示。

如果你剛好使用了這個(gè)圓形Imageview庫(kù)或者其他的一些自定義的圓形Imageview,而你又剛好設(shè)置了占位的話,那么,你就會(huì)遇到第一個(gè)問(wèn)題。如何解決呢?

方案一

不設(shè)置占位圖

方案二

使用Glide的Transformation API自定義圓形Bitmap的轉(zhuǎn)換

? /**

? ? * 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);

方案三

重寫(xiě)Glide的圖片加載監(jiān)聽(tīng)方法,具體如下:

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?

}

});

注意事項(xiàng):

該方法在listview上復(fù)用有問(wèn)題的bug,如果在listview中加載CircleImageView,請(qǐng)不要使用該方法。

方案四:不使用Glide的默認(rèn)動(dòng)畫(huà):

Glide.with(mContext)

? ? .load(url)

? ? .dontAnimate()

? ? .placeholder(R.drawable.loading_drawable)

? ? .into(circleImageview);

(14)json數(shù)據(jù)解析問(wèn)題:json串頭部出現(xiàn)字符:"\ufeff" 解決方法

異常信息

org.json.JSONException: Value? of type java.lang.String cannot be converted to JSONObject

解析服務(wù)器返回 的json格式數(shù)據(jù)時(shí),我們可能會(huì)發(fā)現(xiàn),數(shù)據(jù)格式上是沒(méi)有問(wèn)題的,但是仔細(xì)對(duì)比會(huì)發(fā)現(xiàn),在json串頭部發(fā)現(xiàn)字符:"\ufeff"

客戶端解決方案:

/**

* 異常信息:org.json.JSONException: Value? of type java.lang.String cannot be converted to JSONObject

* json串頭部出現(xiàn)字符:"\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;? ?

? ? ? ? }

}

服務(wù)器端解決方案:

將輸出此json的php源碼重新用editplus之類用utf-8無(wú)BOM的編碼保存。不要用windows系統(tǒng)自帶的記事本編輯php源碼,這個(gè)BOM就是記事本這些windows自帶的編輯器引入的。

(15)Android studio編譯問(wèn)題:not found ndk()

問(wèn)題

Error:(15, 0) Gradle DSL method not found: 'ndk()' method-not-found-ndk

解決方案

出現(xiàn)該問(wèn)題,可能是由于ndk配置在build.gradle配置文件中位置弄錯(cuò)導(dǎo)致的

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導(dǎo)入其他的項(xiàng)目:UnsupportedMethodException

問(wèn)題

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.


錯(cuò)誤截圖

解決方案

將根目錄中的build.gradle文件中的gradle版本號(hào),出現(xiàn)錯(cuò)誤之前,我的是1.3.0,修改成2.2.0之后重新編譯一下就可以運(yùn)行了。

dependencies {? ?

classpath 'com.android.tools.build:gradle:1.3.0'?

}

將這個(gè)版本號(hào)改成你其他項(xiàng)目能夠運(yùn)行成功的版本號(hào)即可

(17)Android studio更新到2.1.1之后使用CollapsingToolbarLayout出現(xiàn)Error inflating class CollapsingToolbarLayout

之前在項(xiàng)目中使用了CollapsingToolbarLayout,效果還是可以的,但是Android stuido更新到2.1.1版本之后出現(xiàn)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)

解決方案

在項(xiàng)目的build.gradle文件中添加下面一行,同步一下即可

compile ('com.android.support:support-v4:23.4.0'){

? ? force = true;

}

StackOverFlow解決方案

(18)Android studio gradle編譯異常

java.lang.UnsupportedClassVersionError: com/android/build/gradle/AppPlugin : Unsupported major.minor version 52.0

很顯然是class版本不支持。經(jīng)查詢,AndroidStudio2.2必須使用JDK8及以上版本,而且是強(qiáng)制的。

所以呢,趕緊下了個(gè)JDK8最新版的。安裝完畢,把JAVA_HOME指向了JDK8,實(shí)測(cè)JDK7和8是可以共存的。

那么,重啟Android Studio后問(wèn)題解決,Build Successful !

(19)電腦突然斷電,Android studio 工程代碼全部報(bào)錯(cuò),找不到android sdk 的依賴包,clean、重啟都沒(méi)有用

前幾天公司搬家,正準(zhǔn)備同步代碼,突然斷電、等把電腦搬到新辦公樓,打開(kāi)AS發(fā)現(xiàn)所有的項(xiàng)目代碼報(bào)錯(cuò),找不到android 依賴包,clean、重啟都沒(méi)有用,

(20)recycleview嵌套列表項(xiàng)顯示不全問(wèn)題

解決方案:

第一個(gè)RecyclerView的Adapter(即父RecyclerView):

@Override?

? ? public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {?

? ? ? ? ? ? ? View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shop_item,null); 解決條目顯示不全?

? ? ? ? ? ? ? MyHolder holder = new MyHolder(view);?

? ? ? ? ? ? ? ? ? return holder;?

? ? }?

第二個(gè)RecyclerView的Adapter(即子RecyclerView):

@Override?

public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {?

? ? View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.check_item, parent,false);//解決寬度不能鋪滿?

? MyHolder holder = new MyHolder(view);?

? ? ? return holder;?

? }?

(21)Android手機(jī)真機(jī)調(diào)試,日志不打印的解決方案:

1、在撥號(hào)界面輸入:*#*#2846579#*#* 進(jìn)入測(cè)試菜單界面。

2、Project Menu–后臺(tái)設(shè)置–LOG設(shè)置

3、LOG開(kāi)關(guān)–LOG打開(kāi) LOG級(jí)別設(shè)置–VERBOSE

4、Dump&Log– 全部選中

5、重啟手機(jī)

(22)java.lang.IndexOutOfBoundsException Inconsistency detected. Invalid item position 2(offset:2).state:4

解決方案:Recyclerview在下拉刷新時(shí),如果在數(shù)據(jù)沒(méi)更新到之前將list? clear? 之后,迅速滑動(dòng)會(huì)造成crash,所以一般在下拉刷新之前,等數(shù)據(jù)刷新回來(lái)再把之前的數(shù)據(jù)進(jìn)行清除。

(23)**使用友盟分享——微信、朋友圈分享出現(xiàn)java.lang.NoClassDefFoundError: org.apache.http.entity.mime.MultipartEntity

**

解決方案:造成這樣的原因是因?yàn)槿鄙賖ttpmime_jar,添加是httpmime_jar包之后即可正常分享

(24)Fragment中調(diào)用getActivity()出現(xiàn)空指針異常

解決方案:

? 對(duì)于上面的問(wèn)題,可以考慮下面這兩種解決辦法:

? 1、不保存fragment的狀態(tài):在MyActivity中重寫(xiě)onSaveInstanceState方法,將super.onSaveInstanceState(outState);注釋掉,讓其不再保存Fragment的狀態(tài),達(dá)到fragment隨MyActivity一起銷毀的目的。

? 2、重建時(shí)清除已經(jīng)保存的fragment的狀態(tài):在恢復(fù)Fragment之前把Bundle里面的fragment狀態(tài)數(shù)據(jù)給清除。方法如下:

? ? ? if(savedInstanceState!= null)

? ? ? {

? ? ? ? ? String FRAGMENTS_TAG = "android:support:fragments";

? ? ? ? ? savedInstanceState.remove(FRAGMENTS_TAG);

? ? ? }

(25)RecyclerView嵌套使用切換頁(yè)面出現(xiàn)自動(dòng)滾動(dòng)問(wèn)題

原因:

造成這樣的原因是由于子RecyclerView搶占焦點(diǎn)導(dǎo)致的,如果你去查看RecyclerView的源碼會(huì)發(fā)現(xiàn),它會(huì)在構(gòu)造方法中調(diào)用setFocusableInTouchMode(true),所以,設(shè)為false可以解決這個(gè)問(wèn)題。

解決方案

在子RecyclerView中調(diào)用如下方法

? ? ? ? ? //設(shè)置焦點(diǎn)不需要

? ? ? ? ? secondRvList.setFocusableInTouchMode(false);

? ? ? ? ? secondRvList.requestFocus();

(26)Android 7.0設(shè)備拍照閃退問(wèn)題

原因:

Android 7.0 做了一些系統(tǒng)權(quán)限更改,為了提高私有文件的安全性,面向 Android 7.0 或更高版本的應(yīng)用私有目錄被限制訪問(wèn),此設(shè)置可防止私有文件的元數(shù)據(jù)泄漏,如它們的大小或存在性。而此權(quán)限更改有多重副作用,其中之一就是當(dāng)傳遞軟件包網(wǎng)域外的 file:// URI 可能給接收器留下無(wú)法訪問(wèn)的路徑。因此,嘗試傳遞 file:// URI 會(huì)觸發(fā) FileUriExposedException。分享私有文件內(nèi)容的推薦方法是使用 FileProvider。在應(yīng)用間共享文件對(duì)于面向 Android 7.0 的應(yīng)用,Android 框架執(zhí)行的 StrictMode API 政策禁止在您的應(yīng)用外部公開(kāi) file:// URI。如果一項(xiàng)包含文件 URI 的 intent 離開(kāi)您的應(yīng)用,則應(yīng)用出現(xiàn)故障,并出現(xiàn) FileUriExposedException 異常。要在應(yīng)用間共享文件,應(yīng)發(fā)送一項(xiàng) content:// URI,并授予 URI 臨時(shí)訪問(wèn)權(quán)限。進(jìn)行此授權(quán)的最簡(jiǎn)單方式是使用 FileProvider 類。點(diǎn)擊查看Android官方說(shuō)明

解決方案

1.在清單文件添加如下代碼

? ? <provider

? ? ? ? ? android:name="android.support.v4.content.FileProvider"

? ? ? ? ? android:authorities="你的應(yīng)用包名.fileProvider"

? ? ? ? ? android:exported="false"

? ? ? ? ? android:grantUriPermissions="true">

? ? ? ? ? <meta-data

? ? ? ? ? ? ? android:name="android.support.FILE_PROVIDER_PATHS"

? ? ? ? ? ? ? android:resource="@xml/provider_paths"/>

? ? ? </provider>

android:authorities="com.alex.demo.FileProvider" 自定義的權(quán)限?

android:exported="false" 是否設(shè)置為獨(dú)立進(jìn)程?

android:grantUriPermissions="true" 是否擁有共享文件的臨時(shí)權(quán)限?

android:resource="@xml/external_storage_root" 共享文件的文件根目錄,名字可以自定義?

2.在xml文件夾目錄下新建provider_paths文件,名字自定義,添加如下代碼

<?xml version="1.0" encoding="utf-8"?>

<resources>

? <paths>

? ? ? <external-path

? ? ? ? ? name="camera_photos"

? ? ? ? ? path="" />

? </paths>

</resources>

3.調(diào)用系統(tǒng)相機(jī)處代碼處理

? //調(diào)用系統(tǒng)相機(jī)拍照

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, parUri(tempFile));

startActivityForResult(cameraIntent, REQUEST_CAMERA);

}

? /**

? ? * 生成uri

? ? *

? ? * @param cameraFile

? ? * @return

? ? */

? private Uri parUri(File cameraFile) {

? ? ? Uri imageUri;

? ? ? String authority = getContext().getPackageName()+ ".provider";

? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

? ? ? ? ? //通過(guò)FileProvider創(chuàng)建一個(gè)content類型的Uri

? ? ? ? ? imageUri = FileProvider.getUriForFile(getContext(), authority, cameraFile);

? ? ? } else {

? ? ? ? ? imageUri = Uri.fromFile(cameraFile);

? ? ? }

? ? ? return imageUri;

? }

(27)使用Glide加載列表項(xiàng),刷新之后圖片大小出現(xiàn)縮放問(wèn)題

原因:導(dǎo)致這樣的問(wèn)題是因?yàn)镮mageView的默認(rèn)資源大小和下載資源大小不一樣。

解決方案:

(1)加載與Imageview 設(shè)置的寬高一致的圖片,有的圖片地址后面可以拼接對(duì)應(yīng)的分辨率大小,然后根據(jù)傳的分辨率大小來(lái)下載圖片;

(2) 代碼里面再設(shè)置一下ImageView的大小,然后再加載圖片

/**

*

*此處的MyBitmapImageViewTarget 為自定義的BitmapImageViewTarget,在里面獲取imagview的寬高

**/

Glide.with(context)

.load(url)

.asBitmap()

.centerCrop()

.placeholder(loadingPic)

.error(errorPic)

.into(new MyBitmapImageViewTarget(imageView));

(3)禁止Glide的默認(rèn)加載動(dòng)畫(huà),也可以解決這個(gè)問(wèn)題

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容