错误找不到片段构造函数

发布于 2025-01-14 03:35:43 字数 6345 浏览 0 评论 0原文

一些用户遇到“无法找到片段构造函数”错误 但我无法在我的模拟器和测试设备上重现此错误

我阅读了这些资源 [找不到 Fragment 构造函数][找不到片段构造函数(设置活动)][无法找到 Fragment 构造函数 kotlin] 但这没有帮助。

所有资源都告诉我向片段添加一个空构造函数,但在我的情况下,每个片段都有一个空构造函数。

设置工具栏共享stackTrace和

stackTrace

java.lang.RuntimeException: Unable to start activity ComponentInfo{{packageName}/{packageName}.presentation.main.MainActivity}: androidx.fragment.app.o$d: Unable to instantiate fragment a9.a: could not find Fragment constructor
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2805)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2883)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4726)
    at android.app.ActivityThread.-wrap18(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1619)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6523)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
Caused by: androidx.fragment.app.o$d: Unable to instantiate fragment a9.a: could not find Fragment constructor
    at androidx.fragment.app.c0$b.a(:5)
    at androidx.fragment.app.h0.a(Unknown Source:2)
    at androidx.fragment.app.i0.<init>(Unknown Source:13)
    at androidx.fragment.app.c0.W(:8)
    at androidx.fragment.app.c0.b(:23)
    at androidx.fragment.app.u.lambda$init$1(:1)
    at androidx.fragment.app.u.i(Unknown Source:0)
    at androidx.fragment.app.t.a(Unknown Source:2)
    at androidx.activity.ComponentActivity.onCreate(:1)
    at androidx.fragment.app.u.onCreate(Unknown Source:0)
    at {packageName}.presentation.main.MainActivity.onCreate(Unknown Source:0)
    at android.app.Activity.performCreate(Activity.java:7013)
    at android.app.Activity.performCreate(Activity.java:7004)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:2)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2758)
    ... 10 more
Caused by: java.lang.NoSuchMethodException: <init> []
    at java.lang.Class.getConstructor0(Class.java:2320)
    at java.lang.Class.getConstructor(Class.java:1725)
    ... 26 more

MainActivity

  • 我使用“com.google.android.material.bottomnavigation.BottomNavigationView”
  • 链接到导航图(共4个片段)
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val navFragment = supportFragmentManager.findFragmentById(R.id.mainActivityContainer) as NavHostFragment
        val navController = navFragment.navController
        binding.bottomNavi.setupWithNavController(navController)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        this.intent = intent
    }

    override fun onResume() {
        super.onResume()
        val dest = intent.getStringExtra("destination")
        dest?.let {
            when (it) {
                "mypage" -> { binding.bottomNavi.selectedItemId = R.id.myPageFragment }
                "lounge" -> { binding.bottomNavi.selectedItemId = R.id.loungeFragment }
                "alarm" -> { binding.bottomNavi.selectedItemId = R.id.alarmFragment }
            }
            intent.removeExtra("destination")
        }
    }
}

HomeFragment

  • 注入Glide RequestManager
  • 在MainActivity布局中
  • 下面的代码HouseContentAction是Interface
import androidx.fragment.app.viewModels

@AndroidEntryPoint
class HomeFragment: Fragment(), HouseContentAction {
    private var _binding: FragmentMainHomeBinding? = null
    private val binding get() = _binding!!
    private val viewModel: HomeViewModel by viewModels()
    @Inject lateinit var glide: RequestManager

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentMainHomeBinding.inflate(layoutInflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        ...
        setUpToolBar()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun setUpToolBar() {
        (requireActivity() as AppCompatActivity).setSupportActionBar(binding.homeToolbar)
        (requireActivity() as AppCompatActivity).supportActionBar?.setIcon(AppCompatResources.getDrawable(requireContext(), R.drawable.head_logo))
        (requireActivity() as AppCompatActivity).supportActionBar?.title = ""
    }

LoungeFragment

@AndroidEntryPoint
class LoungeFragment:Fragment(), LoungeContentActions, LoungeActions {
    private var _binding: FragmentMainLoungeBinding? = null
    private val binding get() = _binding!!
    private val viewModel: LoungeViewModel by viewModels()
    @Inject lateinit var glide: RequestManager

    override fun onCreateView() {
        //same as above
    }

    override fun onViewCreated() {
        //same as above, but no toolbar setting
    }

    override fun onDestroyView() {
        //same as above
    }

MyPageFragment

与上面相同,但是没有工具栏

AlarmFragment

与上面相同,但没有工具栏设置并且没有注入滑行

some users are suffering from 'could not find Fragment constructor' error
but I can't reproduce this error on my emulator and test device

I read these resources
[Could not find Fragment constructor], [Could not find fragment constructor (settings activity)], [Could not find Fragment constructor kotlin]
but it didn't help.

All the resource tells me to add an empty constructor to the fragment, but in my case every fragment has an empty constructor.

I Share stackTrace and codes below

stackTrace

java.lang.RuntimeException: Unable to start activity ComponentInfo{{packageName}/{packageName}.presentation.main.MainActivity}: androidx.fragment.app.o$d: Unable to instantiate fragment a9.a: could not find Fragment constructor
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2805)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2883)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4726)
    at android.app.ActivityThread.-wrap18(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1619)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6523)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
Caused by: androidx.fragment.app.o$d: Unable to instantiate fragment a9.a: could not find Fragment constructor
    at androidx.fragment.app.c0$b.a(:5)
    at androidx.fragment.app.h0.a(Unknown Source:2)
    at androidx.fragment.app.i0.<init>(Unknown Source:13)
    at androidx.fragment.app.c0.W(:8)
    at androidx.fragment.app.c0.b(:23)
    at androidx.fragment.app.u.lambda$init$1(:1)
    at androidx.fragment.app.u.i(Unknown Source:0)
    at androidx.fragment.app.t.a(Unknown Source:2)
    at androidx.activity.ComponentActivity.onCreate(:1)
    at androidx.fragment.app.u.onCreate(Unknown Source:0)
    at {packageName}.presentation.main.MainActivity.onCreate(Unknown Source:0)
    at android.app.Activity.performCreate(Activity.java:7013)
    at android.app.Activity.performCreate(Activity.java:7004)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:2)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2758)
    ... 10 more
Caused by: java.lang.NoSuchMethodException: <init> []
    at java.lang.Class.getConstructor0(Class.java:2320)
    at java.lang.Class.getConstructor(Class.java:1725)
    ... 26 more

MainActivity

  • using 'com.google.android.material.bottomnavigation.BottomNavigationView'
  • linked with navigation graph(total 4 fragment)
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val navFragment = supportFragmentManager.findFragmentById(R.id.mainActivityContainer) as NavHostFragment
        val navController = navFragment.navController
        binding.bottomNavi.setupWithNavController(navController)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        this.intent = intent
    }

    override fun onResume() {
        super.onResume()
        val dest = intent.getStringExtra("destination")
        dest?.let {
            when (it) {
                "mypage" -> { binding.bottomNavi.selectedItemId = R.id.myPageFragment }
                "lounge" -> { binding.bottomNavi.selectedItemId = R.id.loungeFragment }
                "alarm" -> { binding.bottomNavi.selectedItemId = R.id.alarmFragment }
            }
            intent.removeExtra("destination")
        }
    }
}

HomeFragment

  • inject Glide RequestManager
  • setup toolbar in MainActivity layout
  • HouseContentAction is Interface
import androidx.fragment.app.viewModels

@AndroidEntryPoint
class HomeFragment: Fragment(), HouseContentAction {
    private var _binding: FragmentMainHomeBinding? = null
    private val binding get() = _binding!!
    private val viewModel: HomeViewModel by viewModels()
    @Inject lateinit var glide: RequestManager

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentMainHomeBinding.inflate(layoutInflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        ...
        setUpToolBar()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun setUpToolBar() {
        (requireActivity() as AppCompatActivity).setSupportActionBar(binding.homeToolbar)
        (requireActivity() as AppCompatActivity).supportActionBar?.setIcon(AppCompatResources.getDrawable(requireContext(), R.drawable.head_logo))
        (requireActivity() as AppCompatActivity).supportActionBar?.title = ""
    }

LoungeFragment

@AndroidEntryPoint
class LoungeFragment:Fragment(), LoungeContentActions, LoungeActions {
    private var _binding: FragmentMainLoungeBinding? = null
    private val binding get() = _binding!!
    private val viewModel: LoungeViewModel by viewModels()
    @Inject lateinit var glide: RequestManager

    override fun onCreateView() {
        //same as above
    }

    override fun onViewCreated() {
        //same as above, but no toolbar setting
    }

    override fun onDestroyView() {
        //same as above
    }

MyPageFragment

same as above, but no toolbar

AlarmFragment

same as above, but no toolbar setting and no inject glide

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文