Q1: Gradle version 2.10 is required. Current version is 3.3
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)
A1:
- 在项目中选择
File - Project structure
,如图:
- 选择
Project
,设置Gradle version
和Android Plugin Version
对应,如图:
例如:Gradle Version:
3.3
目前对应的是(2017-07-19) Android Plugin version:2.3.3
Q2: Gradle's dependency cache may be corrupt
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)
A2:
- 确保项目中的
gradle-wrapper.properties
中的distributionUrl
跟本地的 gradle 版本一致 - 确保项目中的 build.gradle 中的
compileSdkVersion
、buildToolsVersion
、targetSdkVersion
相对应,并且与compile 'com.android.support:appcompat-v7:xxx'
相对应
Q3: 通过 Android Studio 在小米手机上安装 App 报错:It is possible that this issue is resolved by uninstalling an existi
A3:
- 在
Android Studio - Preferences - Build,Execution,Deployment - Instant Run
中,取消勾选Enable Instant Run to hot swap code/resource changes on deploy(default enabled)
- 在小米手机的开发者模式中
开启 USB 调试
和USB 安装
- 在小米手机的开发者模式中
关闭最底部的 MIUI 优化
Q4: MainActivity 跳转到其他 Activity 之后被销毁
A4:
- 在手机的
开发者选项
中,取消不保留活动
的选项,如图:
Q5: failed to find build tools revision 21.1.2
A5:
- 用
Android Studio
创建一个新项目 - 修改报错项目中所有的
build.gradle
文件中的compileSdkVersion
、buildToolsVersion
、minSdkVersion
、targetSdkVersion
与新项目对应 - 修改报错项目中所有的
build.gradle
文件中的compile 'com.android.support:support-v4:xx+'
和compile 'com.android.support:appcompat-v7:xx+'
与新项目对应
Q6: 微信登录返回-6 (在App中点击微信登录后,部分设备会显示一片空白)
原因:因为App的签名跟微信开放平台的应用签名不一致
A6:
- 使用微信的签名工具获取手机中App的签名(需要区分 debug 和 release 模式)
- 修改微信开放平台的应用签名
Q7: webview 的 onJsAlert 方法只调用一次
代码如下:
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
GMLog.e("-->> onJsAlert url:" + url + " message:" + message);
return true;
}
A7:
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
GMLog.e("-->> onJsAlert url:" + url + " message:" + message);
//加入这行代码即可
result.cancel();
return true;
}
Q8: 设置 webView 的 Http cookie,保持登录状态
webView 加载之后,如果有 HTTP_COOKIE
并且有 USER_ID
和 USER_TOKEN
数据,则设置 cookie 成功,如图:
我照网上的方法设置之后,加载 webView 没有 HTTP_COOKIE
:
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(url, cookies);
CookieSyncManager.getInstance().sync();
A8:
如上图,我这里有两个字段:USER_ID
和 USER_TOKEN
,因此需要把后台返回的 cookie 字符串拆分,再分开设置即可:
String cookie = "USER_ID=14;USER_TOKEN=IIQ-I0gEbeaBr48PP8FI7C7T_mrtvAxo;";
String[] cookies = cookie.split(";");
String USER_ID = cookies[0];
String USER_TOKEN = cookies[1];
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(webViewURL, USER_ID);
cookieManager.setCookie(webViewURL, USER_TOKEN);
CookieSyncManager.getInstance().sync();
附 iOS Swift 版的写法(2017):
//取出的数据:"USER_ID=14;USER_TOKEN=IIQ-I0gEbeaBr48PP8FI7C7T_mrtvAxo;"
let cookies = UserDefaults.standard.object(forKey: GMHttpUtils.cookiesKey)
if cookies != nil {
let request = NSMutableURLRequest.init(url: url!)
request.addValue(cookies as! String, forHTTPHeaderField: "Cookie")
webView?.load(request as URLRequest)
}
Q9: ListView 被自定义的 ViewGroup 拦截了点击事件
前几天在 ListView 中使用了一个第三方的 AndroidTagView,导致 ListView 无法响应点击事件
A9:
- 创建一个类并继承 TagContainerLayout,重写 dispatchTouchEvent 方法,
Demo:https://github.com/hw20101101/TagViewDemo
效果图:
Q10: Failed to resolve: com.android.support:appcompat-v7:25.1.0
Error:(27, 13) Failed to resolve: com.android.support:appcompat-v7:25.1.0
A10:
在 maven 中填写 url "https://maven.google.com" 即可
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
Q11: Error inflating class android.support.design.widget.TabLayout 和 Didn't find class "android.support.design.widget.TabLayout
2019-11-25 12:39:30.934 4072-4072/com.hw.mydemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hw.mydemo, PID: 4072
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hw.mydemo/com.hw.mydemo.MainActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class android.support.design.widget.TabLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3430)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class android.support.design.widget.TabLayout
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class android.support.design.widget.TabLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.TabLayout" on path: DexPathList[[zip file "/data/app/com.hw.mydemo-BocFkQNHy4OPkTuU12uQEQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.hw.mydemo-BocFkQNHy4OPkTuU12uQEQ==/lib/arm64, /system/lib64, /product/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
A11: 导致报错的原因其实在第18行,如下:
Caused by: java.lang.ClassNotFoundException:
Didn't find class "android.support.design.widget.TabLayout"
解决办法:
- 如果项目已启用 AndroidX,那么应该使用
implementation 'com.google.android.material:material:1.1.0-alpha04'
和com.google.android.material.tabs.TabLayout
- 如果项目未启用 AndroidX,那么应该使用
implementation 'com.android.support:design:28.0.0'
和android.support.design.widget.TabLayout