溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android中添加fragment后版本不兼容怎么解決

發(fā)布時間:2022-04-11 15:53:32 來源:億速云 閱讀:297 作者:iii 欄目:編程語言

本篇內(nèi)容介紹了“Android中添加fragment后版本不兼容怎么解決”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

Fragment的產(chǎn)生與介紹

Android運行在各種各樣的設(shè)備中,有小屏幕的手機,超大屏的平板甚至電視。針對屏幕尺寸的差距,很多情況下,都是先針對手機開發(fā)一套App,然后拷貝一份,修改布局以適應(yīng)平板神馬超級大屏的。難道無法做到一個App可以同時適應(yīng)手機和平板么,當(dāng)然了,必須有啊。Fragment的出現(xiàn)就是為了解決這樣的問題。你可以把Fragment當(dāng)成Activity的一個界面的一個組成部分,甚至Activity的界面可以完全有不同的Fragment組成,更帥氣的是Fragment擁有自己的生命周期和接收、處理用戶的事件,這樣就不必在Activity寫一堆控件的事件處理的代碼了。更為重要的是,你可以動態(tài)的添加、替換和移除某個Fragment。

昨天學(xué)習(xí)了下Android的Fragment。按照官網(wǎng)的教程,新建了一個類BlankFragment,繼承自Fragment。然后編譯的時候就出錯了:

Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(26.0.0-beta1) from [com.android.support:design:26.0.0-beta1] AndroidManifest.xml:28:13-41
  is also present at [com.android.support:support-v4:26.1.0] AndroidManifest.xml:28:13-35 value=(26.1.0).
  Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:44 to override.

搜了一下,七七八八說什么的都有。最不靠譜的一個就是說什么把manifest文件里的標(biāo)簽從android:name改成class的??慈罩久黠@是和版本有關(guān)啊!后來終于找到個和版本兼容有關(guān)的解決方案,要在app目錄下的build.gradle里添加如下代碼才可以:

configurations.all {
  resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def requested = details.requested
    if (requested.group == 'com.android.support') {
      if (!requested.name.startsWith("multidex")) {
        details.useVersion '26.0.0-beta1'
      }
    }
  }
}

試了一下確實好了。但不能滿足于此。

注意到,就在添加的這段代碼上面,描述的是工程依賴的庫:

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])
  implementation 'com.android.support:appcompat-v7:26.0.0-beta1'
  implementation 'com.android.support.constraint:constraint-layout:1.0.2'
  implementation 'com.android.support:design:26.0.0-beta1'
  implementation 'com.android.support:support-v4:26.1.0'
  testImplementation 'junit:junit:4.12'
  androidTestImplementation 'com.android.support.test:runner:0.5'
  androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'
}

而這一行下面有紅線:

implementation 'com.android.support:appcompat-v7:26.0.0-beta1'

鼠標(biāo)指上去提示:

All com.android.support libraries must use the exact same version specification 
(mixing versions can lead to runtime crashes). 
Found versions 26.1.0, 26.0.0-beta1. Examples include com.android.support:support-compat:26.1.0 
and com.android.support:animated-vector-drawable:26.0.0-beta1

這不是說的很明白:所有依賴com.android.support的庫必須使用一樣的版本!看看Dependency里,com.android.support:appcompat-v7:26.0.0-beta1和com.android.support:support-v4:26.1.0兩個版本就是矛盾的啊!看一下git的記錄,26.1.0這一行就是新加的,肯定就是新建BlankFragment類的時候AndroidStudio自動添加的。再回頭看看出錯日志,說的不也是同一個意思?

“Android中添加fragment后版本不兼容怎么解決”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI