androidx升级问题整理

1.checkbox radioButton等自定义背景和自带背景重叠问题

  androidx由于刚推出不久,当然也有一些无可避免的坑,这不,今天用低版本的手机(android4.4)一运行,发现所有的checkbox自带的按钮都显示出来了,在xml中是用"android:button=null"竟然不起作用了!

查看源码发现是

public class CheckBox extends CompoundButton {

public CheckBox(Context context) {

this(context, null);

}

public CheckBox(Context context, AttributeSet attrs) {

this(context, attrs, com.android.internal.R.attr.checkboxStyle);

}

public CheckBox(Context context, AttributeSet attrs, int defStyleAttr) {

this(context, attrs, defStyleAttr, 0);

}

public CheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

super(context, attrs, defStyleAttr, defStyleRes);

}

@Override

public CharSequence getAccessibilityClassName() {

return CheckBox.class.getName();

}

}

构造方法里面添加了一个默认的主题,因此自己定义一个主题覆盖掉这个就可以了,因此有了下面的解决方案:


<style name="libs_AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

<item name="toolbarStyle">@style/libs_baseAppTheme

<item name="checkboxStyle">@style/noButtonCheckbox

<item name="radioButtonStyle">@style/noButtonRadioButton

</style>



<style name="noButtonCheckbox" parent="@android:style/Widget.CompoundButton.CheckBox">

<item name="android:background">@null

<item name="buttonCompat">@null


<style name="noButtonRadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">

<item name="android:background">@null

<item name="buttonCompat">@null

</style>

对应问题解决

2.部分手机升级androidX后切换语言不生效问题

升级androidX一个多星期,忽然发现7.0以下部分手机却换语言不生效,但是想想似乎和升级没什么关系,毕竟这个AppCompatActivity

早就已经上线了的,因此先用排除法,打了一个升级androidx之前的包试了一下,果然可以切换。这TMD谷歌也坑人啊,改了啥也不提前说。

于是就去网上一顿查资料,查了半天别人遇到的问题都是显而易见的,分分钟都有解决方案。但是这个语言切换的没发现在哪里。

最后好不容易发现一个国外的帖子:

The link of the latestapp-compatwhich is 1.1.0.

After upgrading my app to the latestapp-compatmy language settings stopped working for phones belowAPI 24(roughly, doesn't work on API 21 and below for sure).

For API 24 and above, I have used theContextWrapperand set thelocalehence works.

My question is if theandroidx.appcompat:appcompat:1.1.0is the stable version why does it work for me inalphaandbetaversions unlike the others here & the questions which I have tried.

After updating AppCompat library to appcompat:1.1.0-alpha03 Locale configuration is not working anymore

Change Locale not work after migrate to Androidx - Talks about the alpha and beta (I am using the latest stable build1.1.0)

Should I wait for an official stable version again and downgrade to the last stable version or which is the efficient way to let google know if any(ofcourse, I know to file a bug)?

回答1:


Edit:

To continue using version 1.1.0 add this below yourattachBaseContext:

Kotlin solution:

overridefunapplyOverrideConfiguration(overrideConfiguration:Configuration?){if(overrideConfiguration !=null) {valuiMode = overrideConfiguration.uiMode        overrideConfiguration.setTo(baseContext.resources.configuration)        overrideConfiguration.uiMode = uiMode    }super.applyOverrideConfiguration(overrideConfiguration)}

Java solution:

@OverridepublicvoidapplyOverrideConfiguration(Configuration overrideConfiguration){if(overrideConfiguration !=null) {intuiMode = overrideConfiguration.uiMode;        overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());        overrideConfiguration.uiMode = uiMode;    }super.applyOverrideConfiguration(overrideConfiguration);}

If you don't need to upgrade to the latestappCompatthen check the old answer. Else use the solution provided by @0101100101 here.

Old Answer:

After spending hours trying, got to know that it might be a bug.

Downgradeto the last stable version and it works flawlessly.

dependencies{implementation'androidx.appcompat:appcompat:1.0.2'//************ DO NOT UPGRADE LANGUAGE ISSUEonAPI23and below *******************//    implementation'androidx.legacy:legacy-support-v4:1.0.0'....}

Meanwhile, I have filed an issue with Google https://issuetracker.google.com/issues/140880275

回答2:


There is an issue in new app compat libraries related to night mode that is causing to override the configuration on android 21 to 25. This can be fixed by applying your configuration when this public function is called:

public void applyOverrideConfiguration(Configuration overrideConfiguration

For me, this little trick has worked by copying the settings from the overridden configuration to my configuration but you can do whatever you want. It is better to reapply your language logic to the new configuration to minimize errors

@OverridepublicvoidapplyOverrideConfiguration(Configuration overrideConfiguration){if(Build.VERSION.SDK_INT >=21&& Build.VERSION.SDK_INT <=25) {//Use you logic to update overrideConfiguration localeLocale locale = getLocale();//your own implementation hereoverrideConfiguration.setLocale(locale);}super.applyOverrideConfiguration(overrideConfiguration);}

握草,我刚好是1.1.0,完美入坑。赶紧按照这个解决方案在baseActivity里面重写了这个方法

@Override

public void applyOverrideConfiguration(Configuration overrideConfiguration) {

// 兼容androidX在部分手机切换语言失败问题

 if (overrideConfiguration !=null) {

int uiMode = overrideConfiguration.uiMode;

 overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());

 overrideConfiguration.uiMode = uiMode;

 }

super.applyOverrideConfiguration(overrideConfiguration);

}

3. Android WebView 5.x 系统下 Resources$NotFoundException异常处理

之前没有见过这个错误,自从升级androidx 到appCompat1.1.0之后,vivo手机5.X一直在崩溃

最近线上后台发现一个崩溃问题,在Android5.x上,创建webview时会发生carsh,报错信息:

Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003    at android.content.res.Resources.getText(Resources.java:318)    at android.content.res.VivoResources.getText(VivoResources.java:123)    at android.content.res.Resources.getString(Resources.java:404)    at com.android.org.chromium.content.browser.ContentViewCore.setContainerView(ContentViewCore.java:694)    at com.android.org.chromium.content.browser.ContentViewCore.initialize(ContentViewCore.java:618)    at com.android.org.chromium.android_webview.AwContents.createAndInitializeContentViewCore(AwContents.java:631)    at com.android.org.chromium.android_webview.AwContents.setNewAwContents(AwContents.java:780)    at com.android.org.chromium.android_webview.AwContents.(AwContents.java:619)    at com.android.org.chromium.android_webview.AwContents.(AwContents.java:556)    at com.android.webview.chromium.WebViewChromium.initForReal(WebViewChromium.java:312)    at com.android.webview.chromium.WebViewChromium.access$100(WebViewChromium.java:96)    at com.android.webview.chromium.WebViewChromium$1.run(WebViewChromium.java:264)

在大部分的vivo 5.x系统下会出现问题,非5.x系统不会出现。

解决办法一

在Android5.x上通过解决自定义WebView

网上大部分都是这种方式解决的。

publicclassLollipopFixedWebViewextendsWebView{publicLollipopFixedWebView(Contextcontext){super(getFixedContext(context));}publicLollipopFixedWebView(Contextcontext,AttributeSetattrs){super(getFixedContext(context),attrs);}publicLollipopFixedWebView(Contextcontext,AttributeSetattrs,intdefStyleAttr){super(getFixedContext(context),attrs,defStyleAttr);}@TargetApi(Build.VERSION_CODES.LOLLIPOP)publicLollipopFixedWebView(Contextcontext,AttributeSetattrs,intdefStyleAttr,intdefStyleRes){super(getFixedContext(context),attrs,defStyleAttr,defStyleRes);}publicLollipopFixedWebView(Contextcontext,AttributeSetattrs,intdefStyleAttr,booleanprivateBrowsing){super(getFixedContext(context),attrs,defStyleAttr,privateBrowsing);}publicstaticContextgetFixedContext(Contextcontext){if(Build.VERSION.SDK_INT>=21&&Build.VERSION.SDK_INT<23)// Android Lollipop 5.0 & 5.1returncontext.createConfigurationContext(newConfiguration());returncontext;}}

主要核心方法是getFixedContext(context),根据版本号配置不同的context.

解决方法二

如果项目中使用"androidx.appcompat:appcompat:1.1.0",替换为"androidx.appcompat:appcompat:1.0.2"

1.1.0版本webview在Android5.x上有问题,恢复到1.0.2之后确实解决了此问题,目前没有具体追踪,后续会跟上。

4.最终使用androidx.appcompat:appcompat:1.1.0-rc01版本解决语言切换和webview崩溃问题。

5.忽然之前的1.1.0-rc01切换语言和webview崩溃问题重新出现,查看了代码版本号还是之前的啊,代码都没改动,这是啥情况啊?一脸懵逼啊

最后在打包的时候Android左边External Library看到有两个appcompat:appcompat,一个是androidx.appcompat:appcompat:1.1.0-rc01一个是androidx.appcompat:appcompat:1.1.0。这个1.1.0是哪里来的?最终对比代码提交记录发现是'com.google.android.material:material:1.1.0'这个下面引入的包。结果android studio就指向1.1.0正式版啦。卧槽!这他妈能不能制定一个版本号啊,最后找到一个设置

// 指定androidx.appcompat使用版本号,

// 此处是为了兼容1. 7.0以下部分手机切换语言失败问题 2. 5.X系统部分手机webview找不到资源问题

configurations.all{

    resolutionStrategy{

        resolutionStrategy.eachDependency{ details->

            if (details.requested.group =='androidx.appcompat') {

details.useVersion"1.1.0-rc01"

            }

}

}

}

重新打包测试,可以啦。记得这个设置放到app下面build.gradle里。

到此问题解决!


暂时分享到这里,其他的遇到了在分享吧。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,099评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,473评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,229评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,570评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,427评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,335评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,737评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,392评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,693评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,730评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,512评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,349评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,750评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,017评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,290评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,706评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,904评论 2 335

推荐阅读更多精彩内容

  • 专业考题类型管理运行工作负责人一般作业考题内容选项A选项B选项C选项D选项E选项F正确答案 变电单选GYSZ本规程...
    小白兔去钓鱼阅读 8,957评论 0 13
  • Web网站测试流程和方法(转载) 1测试流程与方法 1.1测试流程 进行正式测试之前,应先确定如何开展测试,不可盲...
    夏了夏夏夏天阅读 1,271评论 0 0
  • ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。 O...
    我想起个好名字阅读 4,998评论 0 9
  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,465评论 1 11
  • 感恩,早晨收到能断金刚家人的点赞, 愿你的畅益果让更多的人知道并了解,让更多的需要的人购买!并祝家庭幸福,还有你和...
    愛月亮的魚兒爱阅读 131评论 0 2