Android開發錯誤集錦

惜夢發表於2019-12-23

2019年12月23日15:57:41

簡書轉掘金了,簡書廣告太多,受不了了 原鏈

2017年5月5日19:27:55

ButterKnife8.0之後依賴注入之後報空指標錯誤

解決方式:因為8.0之後執行時程式碼外掛更新,所以需要配置

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
複製程式碼

不過最簡單的辦法是除了依賴Butterknife之外,依賴

annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
複製程式碼

2017年5月5日20:00:11

Glide載入圖片報錯You must not call setTag() on a view Glide is targeting

解決方式:出現這種錯誤往往是因為使用了ListView或者RecyclerView,其Item的佈局進行了簡寫,解決方法只需要將Item增加父佈局即可

2017年11月10日10:08:18

Glide載入大圖出現拉伸問題. 首先將自己的imageview的scaleType屬性設定為centerCrop,然後使用glide的時候如下設定

Glide.with(context).load(url).asBitmap().centerCrop().placeholder(R.drawable.defaultpic).into(imageview);
複製程式碼

2018年1月9日16:06:56

Glide V4依賴出錯

Android開發錯誤集錦

提示Support資源找不到,只需要升級一下Support版本就可以,最好為26以上

implementation 'com.android.support:appcompat-v7:27+'
implementation 'com.android.support:design:27.+'
複製程式碼

2018年7月19日14:32:37

Configuration 'compile' is obsolete and has been replaced with 'implementation'. It will be removed at the end of 2018

提示將compile替換為implementation,並且compile將於2018年底進行刪除

2018年7月19日14:38:08

The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin.

instrumentTest在更新之後已經過時,用androidTest替換即可

2018年7月19日14:48:39

DSL element 'DexOptions.incremental' is obsolete and will be removed at the end of 2018

將incremental = true刪除即可.

2018年8月9日13:52:22

Toolbar內部左側始終有一段空白,無法填充

Android開發錯誤集錦

在toolbar佈局中加入如下程式碼即可解決

app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
複製程式碼

2018年8月18日10:34:24

自定義對話方塊頂部始終有一個白邊,底部線性佈局巢狀TextView始終無圓角

白邊問題ImageView新增屬性
android:adjustViewBounds="true"

無圓角問題原因:把圓角shape檔案設定給了線性佈局,修改設定給textview後正常
複製程式碼

2018年8月18日11:20:27

OPPO手機拍照成功之後點選確定沒反應,經過核查發現是儲存圖片的路徑問題導致的,將路徑改為如下後正常

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()
複製程式碼

2018年8月23日11:53:43

Android巢狀H5,通過javascript呼叫H5無法顯示內容,將H5的UL改為div包裹UL恢復正常

2018年10月11日16:20:29

打包混淆之後EventBus報錯,在混淆檔案中加入如下程式碼正常

-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}
複製程式碼

2018年10月19日09:14:51

嘗試使用Dagger2時報錯

Could not find method apt() for arguments [com.google.dagger:dagger-compiler:2.6] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
複製程式碼

將依賴中的apt...改為annotationProcessor...

參考資料 : With android gradle plugin 2.2.0 release, the android-apt plugin is no longer needed for annotation processing. The apt function was included in the latest android gradle plugin which called annotationProcessor.

compile 'com.google.dagger:dagger:2.6'
annotationProcessor "com.google.dagger:dagger-compiler:2.6"
複製程式碼

2018年10月31日11:15:38

Android ContextThemeWrapper cannot be cast to android.app.Activity

加入以下程式碼:

private static Activity checkActivity(Context context) {
    if (context == null){
       return null;
    } else if (context instanceof Activity){
        return (Activity)context ;
    } else if (context instanceof ContextWrapper){
        return checkActivity(((ContextWrapper)context).getBaseContext());
    }
    return null;
}
複製程式碼

呼叫時:

TextView tvView = new TextView(checkActivity(getContext()));
複製程式碼

2018年11月6日09:14:13

去除騰訊X5瀏覽器的滑動塊(原生去除滑動塊方法對X5無效)

if (infoWebView.getX5WebViewExtension() != null) {
            infoWebView.getX5WebViewExtension().setHorizontalScrollBarEnabled(false);//水平不顯示滾動按鈕
            infoWebView.getX5WebViewExtension().setVerticalScrollBarEnabled(false); //垂直不顯示滾動按鈕
        }
複製程式碼

2018年11月17日14:04:17

判斷當前處於活動的Activity是否包含指定Activity

/**
     * 判斷MainActivity是否活動
     *
     * @param activityName 要判斷Activity,最好傳全包名
     * @return boolean
     */
    private boolean isMainActivityAlive(Context context, String activityName) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
        for (ActivityManager.RunningTaskInfo info : list) {
            // 注意這裡的 topActivity 包含 packageName和className,可以列印出來看看
            if (info.topActivity.toString().contains(activityName) || info.baseActivity.toString().contains(activityName)) {
                LogUtils.e(TAG, info.topActivity.getPackageName() + " info.baseActivity.getPackageName()=" + info.baseActivity.getPackageName());
                return true;
            }
        }
        return false;
    }
複製程式碼

2018年11月23日14:30:35

AS專案編譯報錯

Configuration on demand is not supported by the current version of the Android Gradle plugin since you are using Gradle version 4.6 or above. Suggestion: disable configuration on demand by setting org.gradle.configureondemand=false in your gradle.properties file or use a Gradle version less than 4.6.
複製程式碼

在gradle-wrapper.properties 檔案,修改distributionUrl 引數,低於4.6即可

2018年12月18日11:31:06 Android P強制接受HTTP: 新建檔案 res -> xml ->network_security_config.xml,並填寫如下內容:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>
複製程式碼

在AndroidManifest ->application節點加入:

android:networkSecurityConfig="@xml/network_security_config"
複製程式碼

2018年12月18日11:33:57

Android P繼續使用HTTPClient 給老專案升級到P,專案報錯:

java.lang.RuntimeException: Stub!
&emsp;&emsp;at org.apache.http.message.BasicNameValuePair.<init>(BasicNameValuePair.java:6)
複製程式碼

在Android P之後,org.apache.http.legacy 庫將從 bootclasspath 中刪除,所以無法繼續使用.官方文件:

Apache HTTP 客戶端棄用影響採用非標準 ClassLoader 的應用

小聲BB(還能怎麼辦,強制接受吧......) 繼續使用方法:在AndroidManifest ->application節點內加入:

<uses-library
            android:name="org.apache.http.legacy"
            android:required="false"/>
複製程式碼

2019年1月10日11:11:58

報錯 : Manifest merger failed with multiple errors, see logs
複製程式碼

解決方法:切換到Terminal,執行如下命令後即可看到詳細錯誤

gradlew processDebugManifest --stacktrace
複製程式碼

Android開發錯誤集錦

2019年2月25日09:13:49

ERROR: Failed to resolve: support-media-compat
複製程式碼

原因:莫名其妙的報錯,莫名其妙的被牆,莫名其妙的糟心 解決方法: 專案build檔案中註釋:

 mavenCentral()
 google()
 jcenter()
複製程式碼

換為

 maven { url 'https://maven.aliyun.com/repository/google' }
 maven { url 'https://maven.aliyun.com/repository/jcenter' }
 maven { url 'http://maven.aliyun.com/nexus/content/groups/public' } 
複製程式碼

最後應為如下:

buildscript {
    repositories {
//        jcenter()
//        mavenCentral()
//        google()
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
    }
    dependencies {
      ...
    }
}

allprojects {
    repositories {
//        mavenCentral()
//        jcenter()
//        google()
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
        ...
    }
}
複製程式碼

2019年4月15日15:26:46

EditText的clearFocus()方法無效 通過檢視原始碼發現,clearFocus並不是真的清除焦點,而是在整個佈局中遍歷獲取focusInTouchMode為true的View,如果EditText為第一個,就又重新設定了焦點,陷入了死迴圈,所以才會看上去無效,解決方法只需要將EditText之前的view設定如下屬性

android:focusableInTouchMode="true"
複製程式碼

2019年7月26日15:26:45

AndroidManifast警告GoogleAppIndexingWarning App is not indexable by Google Search

Android開發錯誤集錦
解決方法: 按照提示在AndroidManifest.xml檔案中的至少一個頁面中增加如下intent-filter:

<action android:name="android.intent.action.VIEW" />
複製程式碼

或直接進行Alt+Enter忽略

2019年10月22日14:55:29

從後臺切換程式後,頁面重新載入報錯

java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.support.v7.widget.Toolbar$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/toolbar. Make sure other views do not use the same id
複製程式碼

報錯是說頁面重新載入時,toolbar的id是重複的,百思不得其解,百度發現一些相同的問題,無解,後偶然間發現,佈局中include的toolbar的id和toolbar.xml中的toolbar的id都是toolbar,將內部的toolbar的id進行修改後,解決問題.

Android開發錯誤集錦

 <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            binding:toolbarViewModel="@{mainViewModel.toolbarViewModel}" />
複製程式碼
  <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="0dp"
            android:layout_height="49dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
複製程式碼

上邊的id重複了,導致的問題

2019年12月31日09:52:51

老專案升級報錯

NoClassDefFoundError: Failed resolution of: Lorg/apache/http/params/BasicHttpParams
複製程式碼

清單檔案application標籤中加入

 <application
       ...>

        <uses-library android:name="org.apache.http.legacy" android:required="false" />
</application>
複製程式碼

build檔案android下加入

android {
   ...
        useLibrary 'org.apache.http.legacy'
    }
複製程式碼

相關文章