Android 问题整理 一
// AndroidStudio Build Output乱码解决
双击 shift 搜索文件 Edit Custom VM Options 添加代码,重启 as
-Dfile.encoding=UTF-8
参考:AndroidStudio Build Output乱码解决
// Android Studio 删除错误配置
git config --global --unset xxxxx // 删除配置文件中的 xxxxx
// Android 控件 com.google.android.material.tabs.TabLayout tab 分散布局
tabLayout.setTabMode(TabLayout.MODE_FIXED);
// Android Studio 修改 git 提交者账户信息
参考:Android Studio Git 修改用户名、密码、URL
git config --global user.name "wdf"
git config --global user.email “……”
git config --list获取所有信息
// Android Studio 添加git忽略文件
1、增加.gitignore文件,编写要忽略的文件
2、执行命令git rm -r --cached .【后面有一个点】
3、然后再执行git add -A把所有文件添加到版本控制
4、最后执行commit提交就可以了。
// Android控件十三:SwipeRefreshLayout实现上拉加载和下拉刷新
// 下拉刷新和上拉刷新都用SwipeRefreshLayout 自带的进度条
// SwipeRefreshLayout+RecyclerView冲突的几种解决方案
// 服务器资源路径以 xxx.xxx.com/1.0/user/abc 方式访问,.htaccess 的重写规则
参考:Restful的实例_PHP_gaokcl的博客-CSDN博客
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]
// 服务器向用户返回的状态码和提示信息
// Android Studio 3.6.1 无法下载github第三方库,gradle错误。修改项目的 build.gradle
原文:Android Studio 中配置阿里云公共代码库(镜像仓库) – Dkaishu
默认配置:(新建项目后自动生成的内容)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
第一种方法修改后(配合科学上网):
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url 'https://jitpack.io'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
第二种方法修改后:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
maven { url 'https://maven.aliyun.com/repository/central' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/public' }
mavenLocal()
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/central' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/public' }
mavenLocal()
google()
jcenter()
maven {
url 'https://jitpack.io'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
第三种方法修改后:
gradle-wrapper.properties
#Wed Aug 28 14:54:22 CST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
// distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
build.gradle (项目 gradle)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
mavenCentral()
// maven {url "https://dl.bintray.com/rongcloud/maven"}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
C:\Users\Administrator\.gradle\gradle.properties
## For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Sat Apr 25 13:09:07 CST 2020
systemProp.https.proxyPort=52921
#systemProp.http.proxyHost=127.0.0.1
org.gradle.jvmargs=-Xmx2048m -XX\:MaxPermSize\=512m -XX\:+HeapDumpOnOutOfMemoryError -Dfile.encoding\=UTF-8
org.gradle.daemon=true
#systemProp.https.proxyHost=127.0.0.1
org.gradle.configureondemand=true
org.gradle.parallel=true
systemProp.http.proxyPort=52921
android.useAndroidX=true
// Android Studio 3.6.1 build 输出乱码
添加代码 -Dfile.encoding=UTF-8
// Android Studio 3.1 模拟器启动报错 Guest isn’t online after 7 seconds; ADB cannot conne…
打开模拟器管理器,点击报错的模拟器 Actions 中的 Cold Boot Now
// Android Studio 3.1.3 模拟器启动报错 emulator: ERROR: Could not initialize OpenglES emulation, use ‘-gpu off’ to
1.选择 software;2.使用驱动精灵更新电脑显卡驱动。
// 彻底明确怎样设置minSdkVersion和targetSdkVersion
// Android Studio 3.1 编辑 gradle 非常卡
在本机 hosts 中添加
127.0.0.1 search.maven.org
// WheelPicker这个依赖死活加不进项目中
// Cannot resolve symbol ‘AppCompatActivity’ 但是项目可以运行
1.右键项目,Open Module Settings ->
(Project Structure)Modules -> app -> Dependencies(查看你的v7包版本)
2.添加 compile ‘com.android.support:appcompat-v7:23.2.1’ (对应你的版本) 到
build.gradle(Module:app) -> dependencies 中
// as 3.1.2 构建项目时从 jcenter 下载组件很慢,可以尝试下面的方法,参考的地址点击这里——》参考文章
项目结构视图显示为 Android
1. 修改 Gradle Scripts/build.gradle 中
jcenter()
为
jcenter() {url 'http://jcenter.bintray.com/'}
// 用 http 代替 https 下载,我自己用了没有什么作用,还是报错。因为修改jcenter 协议为 http 我发现下载还是显示 https
2. 修改 Gradle Scripts/build.gradle 中
jcenter()
为
maven {url 'http://maven.aliyun.com/nexus/center/groups/public/'}
// 使用阿里的源,快了不少,需要的组件下载到了,但是一部分组件报错“无法解析到”
3. 修改 Gradle Scripts/build.gradle 中
jcenter()
为
// 把阿里的源加进去,项目就没有问题了
jcenter()
maven {url 'http://maven.aliyun.com/nexus/center/groups/public/'}
maven {url 'http://maven.aliyun.com/nexus/center/groups/public/'}
// AS 3.1.2 编译报错:The SourceSet ‘instrumentTest’ is not recognized by the Android Gradle Plugin. 参考的地址点击这里——》点击进入参考文章
原因是这里出问题了 instrumentTest.setRoot(‘tests’) ,
这里的配置中 instrumentTest 已被弃用,不适用于现在的 gradle 版本。
解决办法
用 androidTest 替换 instrumentTest,编译运行即可。
// 自动生成 selector 文件 》》点击进入参考文章
在 res 目录上右键 → Resource Type (选择 Drawable) → Root element (就会显示 selector) → 键入文件名确定(完成)
// Activity 隐藏标题栏
setContentView 之上添加下面的代码
requestWindowFeature(Window.FEATURE_NO_TITLE);
如果添加后没有效果,在setContentView之下添加如下代码
if (getSupportActionBar() != null){
getSupportActionBar().hide();
}
修改 Activity 的主题,MainActivity 要继承 Activity,不然报错
下面这种方法,会去掉标题栏(显示通知栏:时间、电量、手机信号等),界面显示为深灰色。
<activity android:name=".MainActivity" android:theme="@android:style/Theme.NoTitleBar">
下面这种方法,会去掉标题栏(不显示通知栏:时间、电量、手机信号等),界面显示为深灰色。
<activity android:name=".MainActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
下面这种方法,会去掉标题栏(显示通知栏:时间、电量、手机信号等),界面显示为白色。
<activity android:name=".MainActivity" android:theme="@android:style/Theme.Light.NoTitleBar">
下面这种方法,会去掉标题栏(不显示通知栏:时间、电量、手机信号等),界面显示为白色。
<activity android:name=".MainActivity" android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen">
// A Fragment,切换到 B Fragment,B Fragment 中的 onResume 方法没有执行,GitHub 示例
# FragmentTestv2
Fragment life Cycle
项目 SDK 配置如下,AVD 使用 8.0 时,Fragment 生命周期打印异常,使用 transaction.addToBackStack(null); 从 fragment2 返回 fragment 时,onResume 不会执行。
build.gradle(app)
...
compileSdkVersion 27
defaultConfig {
applicationId "app.zowneo.fragmenttestv2"
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
...
项目 SDK 配置如下,AVD 使用 4.2 时,Fragment 生命周期打印正常
build.gradle(app)
...
compileSdkVersion 27
defaultConfig {
applicationId "app.zowneo.fragmenttest"
minSdkVersion 24
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
...
// Android Studio 3.1.2 关联码云,这里只写出我遇到的问题
1、在设置中搜索 gitOSC,如果没有找到,使用 gitee 关键词搜索。
2、搜到后先点击插件的网址链接,然后在进行安装(可能会安装失败,多试几次(我试了4次))
3、安装成功后,需要设置你的码云登陆邮箱(用户名)、密码,登陆后可以正常使用。以下是几个参考链接:
Android/AndroidStudio/idea使用教程之git使用(详细)(码云);
4、上传代码的时候,需要排除一个文件 ExampleInstrumentedTest.java
// Android studio 3.0.1 清除git 提交记录 参考文章; —
1.Checkout
git checkout --orphan latest_branch
2. Add all the files
git add -A
3. Commit the changes
git commit -am "commit message"
4. Delete the branch
git branch -D master
5.Rename the current branch to master
git branch -m master
6.Finally, force update your repository
git push -f origin master
// 新建项目报错 Error:Execution failed for task ‘:app:preDebugAndroidTestBuild’.
Build → Rebuild Project
// Android Studio 使用自带模拟器创建刘海屏,建议使用中文语言方便你找到设置选项
1、下载 Android P。
2、创建好 Android P 模拟器,打开开发者选项。
3、在开发者选项中,找到“模拟器具有凹口的显示屏”,在里面选择你需要的异形屏效果。
// 导入 jpush demo 报错:Could not determine the class-path for interface com.android.builder.model.AndroidProject
1、新建一个项目。
2、拷贝这里的代码到 jpush demo同样的文件中。
// Error:Execution failed for task ‘:fragmentanimation:processDebugResources’.
或者 Error:Execution failed for task ‘:*******:processDebugResources’.
在 fragmentanimation 的 build.gradle 中降低(修改)minSdkVersion 18
// 导入git 上的项目,报错 Please configure Android SDK
其他错误导致:参考文章1 —
File → Invalidate Caches / Restart
// Android studio 3.0.1 运行 app 报错,please select Android SDK
在项目架构窗口中切换SDK的版本。
// android textview 设置指定文字颜色 android TextView 设置部分文字背景色 和 文字颜色
参考:android TextView 设置部分文字背景色 和 文字颜色
private void setMoneyColor(String type, TextView textView, String content) {
String a = "";
if (type.equals("pay")) {
a = content.substring(content.indexOf("XXX") + 2, content.indexOf("X"));
}
if (type.equals("review")) {// str.indexOf("背景");
if (a.contains("XXX")) {
a = "XXX";
} else if (a.contains("XX")) {
a = "XX";
}
}
if(type.equals("refund")) {
}
int fstart = content.indexOf(a);
int fend = fstart + a.length();
SpannableStringBuilder style = new SpannableStringBuilder(content);
style.setSpan(new ForegroundColorSpan(ResUtils.getColor(R.color.pink_light)), fstart, fend, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textView.setText(style);
}
设置多个颜色没有起作用,但可以用来参考:Android中TextView文字设置不同的颜色
content = "aaaaaaaaaaaaaaaaaaaaaXXXX:¥500.00;YYYY:¥10.00。";
SpannableStringBuilder builder = new SpannableStringBuilder(content);
String a1 = content.substring(content.indexOf("XXXX:¥") + 6, content.indexOf(";账")-1);
int fstart1 = content.indexOf(a1);
int fend1 = fstart1 + a1.length();
String a2 = content.substring(content.indexOf("YYYY:¥") + 6, content.indexOf("。"));
int fstart2 = content.indexOf(a2);
int fend2 = fstart2 + a2.length();
//ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色
ForegroundColorSpan pink = new ForegroundColorSpan(ResUtils.getColor(R.color.pink_light));
builder.setSpan(pink, fstart1, fend1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(pink,fstart2, fend2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(builder);
// ANDROID 删除SD卡中的文件和文件夹
public static void delAllFile(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
}
else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
}
}
}
// 获取 iconfont.ttf
在 iconfont.cn 中搜索出来自己需要的图标,添加进自己的购物车,然后下载代码,iconfont.ttf 就在下载的压缩包中。