androidx升級(jí)問題整理

1.checkbox radioButton等自定義背景和自帶背景重疊問題

? androidx由于剛推出不久,當(dāng)然也有一些無(wú)可避免的坑,這不,今天用低版本的手機(jī)(android4.4)一運(yùn)行,發(fā)現(xiàn)所有的checkbox自帶的按鈕都顯示出來(lái)了,在xml中是用"android:button=null"竟然不起作用了!

查看源碼發(fā)現(xiàn)是

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();

}

}

構(gòu)造方法里面添加了一個(gè)默認(rèn)的主題,因此自己定義一個(gè)主題覆蓋掉這個(gè)就可以了,因此有了下面的解決方案:


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

對(duì)應(yīng)問題解決

2.部分手機(jī)升級(jí)androidX后切換語(yǔ)言不生效問題

升級(jí)androidX一個(gè)多星期,忽然發(fā)現(xiàn)7.0以下部分手機(jī)卻換語(yǔ)言不生效,但是想想似乎和升級(jí)沒什么關(guān)系,畢竟這個(gè)AppCompatActivity

早就已經(jīng)上線了的,因此先用排除法,打了一個(gè)升級(jí)androidx之前的包試了一下,果然可以切換。這TMD谷歌也坑人啊,改了啥也不提前說。

于是就去網(wǎng)上一頓查資料,查了半天別人遇到的問題都是顯而易見的,分分鐘都有解決方案。但是這個(gè)語(yǔ)言切換的沒發(fā)現(xiàn)在哪里。

最后好不容易發(fā)現(xiàn)一個(gè)國(guó)外的帖子:

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,完美入坑。趕緊按照這個(gè)解決方案在baseActivity里面重寫了這個(gè)方法

@Override

public void applyOverrideConfiguration(Configuration overrideConfiguration) {

// 兼容androidX在部分手機(jī)切換語(yǔ)言失敗問題

?if (overrideConfiguration !=null) {

int uiMode = overrideConfiguration.uiMode;

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

?overrideConfiguration.uiMode = uiMode;

?}

super.applyOverrideConfiguration(overrideConfiguration);

}

3. Android WebView 5.x 系統(tǒng)下 Resources$NotFoundException異常處理

之前沒有見過這個(gè)錯(cuò)誤,自從升級(jí)androidx 到appCompat1.1.0之后,vivo手機(jī)5.X一直在崩潰

最近線上后臺(tái)發(fā)現(xiàn)一個(gè)崩潰問題,在Android5.x上,創(chuàng)建webview時(shí)會(huì)發(fā)生carsh,報(bào)錯(cuò)信息:

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系統(tǒng)下會(huì)出現(xiàn)問題,非5.x系統(tǒng)不會(huì)出現(xiàn)。

解決辦法一

在Android5.x上通過解決自定義WebView

網(wǎng)上大部分都是這種方式解決的。

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),根據(jù)版本號(hào)配置不同的context.

解決方法二

如果項(xiàng)目中使用"androidx.appcompat:appcompat:1.1.0",替換為"androidx.appcompat:appcompat:1.0.2"

1.1.0版本webview在Android5.x上有問題,恢復(fù)到1.0.2之后確實(shí)解決了此問題,目前沒有具體追蹤,后續(xù)會(huì)跟上。

4.最終使用androidx.appcompat:appcompat:1.1.0-rc01版本解決語(yǔ)言切換和webview崩潰問題。

5.忽然之前的1.1.0-rc01切換語(yǔ)言和webview崩潰問題重新出現(xiàn),查看了代碼版本號(hào)還是之前的啊,代碼都沒改動(dòng),這是啥情況?。恳荒樸卤瓢?/p>

最后在打包的時(shí)候Android左邊External Library看到有兩個(gè)appcompat:appcompat,一個(gè)是androidx.appcompat:appcompat:1.1.0-rc01一個(gè)是androidx.appcompat:appcompat:1.1.0。這個(gè)1.1.0是哪里來(lái)的?最終對(duì)比代碼提交記錄發(fā)現(xiàn)是'com.google.android.material:material:1.1.0'這個(gè)下面引入的包。結(jié)果android studio就指向1.1.0正式版啦。臥槽!這他媽能不能制定一個(gè)版本號(hào)啊,最后找到一個(gè)設(shè)置

// 指定androidx.appcompat使用版本號(hào),

// 此處是為了兼容1. 7.0以下部分手機(jī)切換語(yǔ)言失敗問題 2. 5.X系統(tǒng)部分手機(jī)webview找不到資源問題

configurations.all{

? ? resolutionStrategy{

? ? ? ? resolutionStrategy.eachDependency{ details->

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

details.useVersion"1.1.0-rc01"

? ? ? ? ? ? }

}

}

}

重新打包測(cè)試,可以啦。記得這個(gè)設(shè)置放到app下面build.gradle里。

到此問題解決!


暫時(shí)分享到這里,其他的遇到了在分享吧。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 專業(yè)考題類型管理運(yùn)行工作負(fù)責(zé)人一般作業(yè)考題內(nèi)容選項(xiàng)A選項(xiàng)B選項(xiàng)C選項(xiàng)D選項(xiàng)E選項(xiàng)F正確答案 變電單選GYSZ本規(guī)程...
    小白兔去釣魚閱讀 10,460評(píng)論 0 13
  • Web網(wǎng)站測(cè)試流程和方法(轉(zhuǎn)載) 1測(cè)試流程與方法 1.1測(cè)試流程 進(jìn)行正式測(cè)試之前,應(yīng)先確定如何開展測(cè)試,不可盲...
    夏了夏夏夏天閱讀 1,366評(píng)論 0 0
  • ORA-00001: 違反唯一約束條件 (.) 錯(cuò)誤說明:當(dāng)在唯一索引所對(duì)應(yīng)的列上鍵入重復(fù)值時(shí),會(huì)觸發(fā)此異常。 O...
    我想起個(gè)好名字閱讀 5,918評(píng)論 0 9
  • ??JavaScript 與 HTML 之間的交互是通過事件實(shí)現(xiàn)的。 ??事件,就是文檔或?yàn)g覽器窗口中發(fā)生的一些特...
    霜天曉閱讀 3,678評(píng)論 1 11
  • 感恩,早晨收到能斷金剛家人的點(diǎn)贊, 愿你的暢益果讓更多的人知道并了解,讓更多的需要的人購(gòu)買!并祝家庭幸福,還有你和...
    愛月亮的魚兒愛閱讀 150評(píng)論 0 2

友情鏈接更多精彩內(nèi)容