片段事务中的实例化错误

Posted

技术标签:

【中文标题】片段事务中的实例化错误【英文标题】:Instantiate Error in Fragment Transaction 【发布时间】:2021-11-23 17:12:23 【问题描述】:

我在 Fragment Transaction 中遇到问题。 我想更改单击按钮时显示的片段 但是当我尝试单击它时,应用程序会关闭。

它一直显示错误消息, 二进制 XML 文件第 6 行:错误膨胀,以及 无法实例化片段确保类名存在,是公共的,并且有一个公共的空构造函数

我尝试添加 newInstace() 代码,但仍然无法正常工作

下面是错误代码和我的活动,片段代码。我不知道出了什么问题... 请帮帮我。

E/androidRuntime: FATAL EXCEPTION: main
    Process: com.example.illo, PID: 10709
    android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class fragment
    Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
    Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.illo.WritePost1_Fragment: make sure class name exists, is public, and has an empty constructor that is public
        at android.app.Fragment.instantiate(Fragment.java:538)
        at android.app.FragmentContainer.instantiate(FragmentContainer.java:53)
        at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3547)
        at android.app.FragmentController.onCreateView(FragmentController.java:102)
        at android.app.Activity.onCreateView(Activity.java:6323)
        at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at com.example.illo.WritePost_Step2_Fragment.onCreateView(WritePost_Step2_Fragment.kt:27)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.illo.WritePost1_Fragment" on path: DexPathList[[zip file "/data/app/com.example.illo-8XrFiUkhowzrFMYYQ7X5qw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.illo-8XrFiUkhowzrFMYYQ7X5qw==/lib/x86, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.Fragment.instantiate(Fragment.java:524)
        at android.app.FragmentContainer.instantiate(FragmentContainer.java:53) 
        at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3547) 
        at android.app.FragmentController.onCreateView(FragmentController.java:102) 
        at android.app.Activity.onCreateView(Activity.java:6323) 
        at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:300) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at com.example.illo.WritePost_Step2_Fragment.onCreateView(WritePost_Step2_Fragment.kt:27) 
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963) 
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518) 
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282) 
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189) 
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100) 
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002) 
        at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
class WritePostActivity : AppCompatActivity() 
    // step 프래그먼트 가져오기
    lateinit var step1Fragment : WritePost_Step1_Fragment
    lateinit var step2Fragment : WritePost_Step2_Fragment

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_write_post)

        // 툴바 설정
        toolbarSetting()

        // 처음에는 step1로 설정
        ShowStep1()

        // step 이동 버튼
        btn_step1.setOnClickListener 
            ShowStep1()
            Toast.makeText(this@WritePostActivity, "Write Post Step1", Toast.LENGTH_SHORT).show()
        
        btn_step2.setOnClickListener 
            ShowStep2()
            Toast.makeText(this@WritePostActivity, "Write Post Step2", Toast.LENGTH_SHORT).show()

        

        // 글 쓰기 완료 버튼! (누르면 게시글이 등록됨)
        btn_write_post_ok.setOnClickListener 
            // *----서버/DB 필요----*
            // 포스트로 등록
        
    

    // 툴바 세팅
    fun toolbarSetting() 
        // 툴바로 세팅
        setSupportActionBar(toolbar_write_post)
        supportActionBar?.setDisplayShowCustomEnabled(true)
        supportActionBar?.setDisplayShowTitleEnabled(false)
        supportActionBar?.setHomeAsUpIndicator(R.drawable.icon_to_left);    // 뒤로 가기 버튼 커스텀
        supportActionBar?.setDisplayHomeAsUpEnabled(true)   // 뒤로 가기 버튼 자동 생성

        // 뒤로가기 버튼 설정
        toolbar_write_post.setNavigationOnClickListener 
            finish()    // 액티비티 끝내기
            overridePendingTransition(R.anim.no_animation, R.anim.slide_out_left)   // 애니메이션
        

    

    fun ShowStep1()
        step1Fragment = WritePost_Step1_Fragment()
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_write_post, step1Fragment)
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .commit()
    

    fun ShowStep2()
        step2Fragment = WritePost_Step2_Fragment()
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_write_post, step2Fragment)
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .commit()
    


class WritePost_Step2_Fragment : Fragment() 

    companion object 
        fun newInstance(foo: Int): WritePost_Step2_Fragment 
            val args= Bundle()
            args.putInt("foo", foo)
            val fragment= WritePost_Step2_Fragment()
            fragment.arguments= args
            return fragment
        
    

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? 
        val view: View = inflater.inflate(R.layout.fragment_write_post_step2, container, false)


        return view
    


    // Activity의 OnCreate와 같은 역할을 하는 함수
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) 
        Log.d("life_cycle", "F onViewCreated")
        super.onViewCreated(view, savedInstanceState)
    


【问题讨论】:

fragment_write_post_step2 是有效的 xml 文件吗? 【参考方案1】:

你的 xml 文件有错误(特别是第 6 行),首先检查你的 xml 文件,它位于 res/layout 目录中

【讨论】:

我是个傻瓜............谢谢

以上是关于片段事务中的实例化错误的主要内容,如果未能解决你的问题,请参考以下文章

无法实例化类型集群,Mahout 中的 KMean 集群示例

启用 Proguard 后无法实例化片段

创建片段而不从 java 代码实例化它

java 动态片段实例化

java 片段实例化器

Android - 试图实例化一个不是片段的类