Android開發錯誤集錦

2019年12月23日15:57:41

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

2017年5月5日19:27:55

ButterKnife8.0以後依賴注入以後報空指針錯誤php

解決方式:由於8.0以後運行時代碼插件更新,因此須要配置java

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

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

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 targetingapache

解決方式:出現這種錯誤每每是由於使用了ListView或者RecyclerView,其Item的佈局進行了簡寫,解決方法只須要將Item增長父佈局便可api

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依賴出錯bash

提示Support資源找不到,只須要升級一下Support版本就能夠,最好爲26以上app

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 2018less

提示將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內部左側始終有一段空白,沒法填充

在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
複製代碼

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

解決方法: 按照提示在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進行修改後,解決問題.

<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重複了,致使的問題

相關文章
相關標籤/搜索