在Android中实现片段时出错

时间:2019-06-21 15:32:13

标签: android android-fragments kotlin

我目前正在创建一个基本应用程序,试图在android的导航栏中使用片段。每当我运行该应用程序并按相应的菜单项(调用一个片段)时,该应用程序就会崩溃。

我对android开发和Kotlin还是很陌生,目前正在做一个高中设计项目,没有任何编码和应用程​​序开发方面的知识,因此我对很多术语不熟悉。 。这个应用程序主要是根据我从多个YouTube视频中收集的点点滴滴制作而成。我认为此崩溃可能是由于我的经验不足造成的,因此在解决此问题方面将提供任何帮助。

只要我的应用崩溃,这就是我从Logcat获得的信息。

2019-06-22 01:20:30.783 8880-8880/? E/ample.first_tr: Unknown bits set in runtime_flags: 0x8000
2019-06-22 01:20:32.454 8880-9622/com.example.first_try E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
2019-06-22 01:20:32.454 8880-9622/com.example.first_try E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
2019-06-22 01:20:45.570 8880-8880/com.example.first_try E/FragmentManager: No view found for id 0x7f0a0023 (com.example.first_try:id/benefits) for fragment BenefitsFragment{d84c174 #0 id=0x7f0a0023}
2019-06-22 01:20:45.570 8880-8880/com.example.first_try E/FragmentManager: Activity state:
2019-06-22 01:20:45.664 8880-8880/com.example.first_try E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.first_try, PID: 8880
    java.lang.IllegalArgumentException: No view found for id 0x7f0a0023 (com.example.first_try:id/benefits) for fragment BenefitsFragment{d84c174 #0 id=0x7f0a0023}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1454)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7319)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)

这是我MainActivity的一部分(主要只是用于创建导航视图的模板)

  override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.intro -> {
                startActivity(Intent(this, MainActivity::class.java))

            }
            R.id.benefits -> {
                loadBenefits(fragl = BenefitsFragment())

            }
            R.id.connecting-> {


            }
            R.id.socialmedia -> {


            }
            R.id.internetcomms -> {

            }
            R.id.setupandsafety -> {

            }
            R.id.newsmedia -> {

            }
            R.id.fakenews -> {

            }
            R.id.utilities -> {

            }
            R.id.onlineshopping -> {

            }
            R.id.onlinesafety -> {

            }

        }

        drawer_layout.closeDrawer(GravityCompat.START)
        return true
    }
    private fun loadBenefits (fragl: BenefitsFragment) {
        val fm = supportFragmentManager.beginTransaction()
        fm.replace(R.id.benefits, fragl)
        fm.commit()

我认为BenefitsFragment可能与此有关,因为它在Logcat中被提及,所以这里是

package com.example.first_try

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class BenefitsFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return LayoutInflater.from(container?.context).inflate(R.layout.fragment_benefits, container, false)
    }
}

0 个答案:

没有答案