英文:
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'
}
英文:
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<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'
}
答案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<MainFragment>(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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论