![](/img/trans.png)
[英]getSupportFragmentManager().beginTransaction().add(android.R.id.content, f) not working
[英].beginTransaction().add not working in Android Studio
每個人。 我目前正在學習如何在 Android Studio 中切換片段,不幸的是我們的教科書已經過時了。 當我嘗試使用以下代碼時:
if (currentFragment == null) {
val fragment = RootBeer()
supportFragmentManager.beginTransaction().add(R.id.fragmentContainer, fragment).transaction.commit()
}
我收到以下錯誤:
<html>None of the following functions can be called with the arguments supplied:<br/>public open fun add(fragment: Fragment, tag: String?): FragmentTransaction defined in androidx.fragment.app.FragmentTransaction<br/>public open fun add(containerViewId: Int, fragment: Fragment): FragmentTransaction defined in androidx.fragment.app.FragmentTransaction
這是清單代碼:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.ivytech.rootbeerspring2021" >
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/MyTheme.DayNight" >
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ScanBarcodeActivity" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="edu.ivytech.rootbeerspring2021.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/files" />
</provider>
</application>
</manifest>
和我的 fragmentContainer 代碼:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragmentContainer">
</androidx.constraintlayout.widget.ConstraintLayout>
如果還有什么對在這里看到有幫助的,請告訴我。 非常感謝!
使用replace
而不是add
if (fragment != null) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_layout_id, fragment)
transaction.commit()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.