无法理解FragmentManager。

huangapple go评论59阅读模式
英文:

Can't understand with Fragment Manager

问题

我尝试使用SupportFragmentManager添加一个片段,但add函数被标记为红色。这段代码在另一个项目中可以运行。可能是我不理解的地方。整个问题在图片中可见。

"add" 出现问题。

错误:不能使用提供的参数调用以下函数之一。

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.view.WindowCompat
import androidx.fragment.app.commit
import androidx.fragment.app.add  // 这一行有问题
import com.example.homework4_task1.R
import com.example.homework4_task1.presentation.main.fragments.MainFragment

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        fitContentViewToInsets()

        supportFragmentManager.commit {
            add<MainFragment> {
                R.id.container
            }
        }
    }

    private fun fitContentViewToInsets() {
        WindowCompat.setDecorFitsSystemWindows(window, false)
    }
}

依赖项:

dependencies {
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.fragment:fragment-ktx:1.5.5'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

something wrong

英文:

I'm trying to add a fragment using SupportFragmentManager, but the add function is highlighted in red. This code worked in another project. Probably something I don't understand. The whole problem is visible in the picture.

Something is wrong with "add<MainFragment>"

Error: None of the following functions can be called with the arguments supplied.

package com.example.homework4_task1.presentation.main

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.view.WindowCompat
import androidx.fragment.app.commit
import androidx.fragment.app.add
import com.example.homework4_task1.R
import com.example.homework4_task1.presentation.main.fragments.MainFragment

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        fitContentViewToInsets()

        supportFragmentManager.commit {
            add&lt;MainFragment&gt; { 
                R.id.container
            }
        }
    }

    private fun fitContentViewToInsets() {
        WindowCompat.setDecorFitsSystemWindows(window, false)
    }

}
dependencies {

    implementation &#39;androidx.core:core-ktx:1.7.0&#39;
    implementation &#39;androidx.appcompat:appcompat:1.6.1&#39;
    implementation &#39;com.google.android.material:material:1.8.0&#39;
    implementation &#39;androidx.constraintlayout:constraintlayout:2.1.4&#39;
    implementation &#39;androidx.fragment:fragment-ktx:1.5.5&#39;
    testImplementation &#39;junit:junit:4.13.2&#39;
    androidTestImplementation &#39;androidx.test.ext:junit:1.1.5&#39;
    androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.5.1&#39;
}

something wrong

答案1

得分: 0

我认为问题出在你在结尾处使用了一个 lambda 表达式;我能找到的唯一相似之处是:

add<MainFragment>(R.id.container)

实际上这是一个扩展函数,你需要导入

import androidx.fragment.app.add

我认为问题在于 lambda 表达式,因为这意味着你正在传递一个返回 ID (Int @ResId) 的函数。但可用的方法始终需要一个参数作为 id,以便知道在哪里附加碎片。

英文:

I think the problem is you are using a lambda at the end; the only similarity I can find is:

add&lt;MainFragment&gt;(R.id.container)

Which is in fact an extension and you have to import

import androidx.fragment.app.add

I think the problem is the lambda because that would mean you are passing a function that returns an id (Int @ResId). But the available methods will always require a parameter as an id to know where to attach the fragment.

huangapple
  • 本文由 发表于 2023年3月9日 21:02:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定