简体   繁体   中英

.beginTransaction().add not working in Android Studio

everyone. I'm currently learning about switching fragments in Android Studio, and unfortunately our textbook is outdated. When I try to use the following code:


        if (currentFragment == null) {
            val fragment = RootBeer()
            supportFragmentManager.beginTransaction().add(R.id.fragmentContainer, fragment).transaction.commit()

        }

I get the following error:

<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

Here is the manifest code:

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

and my fragmentContainer code:

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

If there's anything else that would be helpful to see here, please just let me know. Thank you so much!

use replace instead of add

    if (fragment != null) {
                val transaction = supportFragmentManager.beginTransaction()
                transaction.replace(R.id.fragment_layout_id, fragment)
                transaction.commit()
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM