我在片段之间切换时遇到错误

问题描述 投票:0回答:1

所以我有一个包含 CameraX 的片段。我想将照片移动到包含 ImageView 的片段。 当我按下相机片段上的照片按钮时

binding.ivCamCapture.setOnClickListener { takePhoto() }

总是有错误:

FATAL EXCEPTION: main
Process: com.dicoding.geotaggingjbg, PID: 23794
java.lang.IllegalArgumentException: No view found for id 0x7f090154 (com.dicoding.geotaggingjbg:id/fragment_save) for fragment SaveFragment{b82c6bd} (df466273-fc83-4df0-8853-dda8508bc87b id=0x7f090154)
androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:547)
androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:272)
androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1943)
androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1839)
androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1782)
androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:565)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

这是拍照的方法:

private fun takePhoto() {
        val imageCapture = imageCapture ?: return
        val photoFile = createCustomTempFile(requireActivity())
        val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile).build()

        imageCapture.takePicture(
            outputOptions,
            ContextCompat.getMainExecutor(requireActivity()),
            object : ImageCapture.OnImageSavedCallback {
                override fun onImageSaved(output: ImageCapture.OutputFileResults) {

                    val imageUri = output.savedUri.toString()
                    val bundle = Bundle().apply {
                        putString(SaveFragment.EXTRA_FILE, imageUri)
                    }
                    showFragment(bundle)
                }

                override fun onError(exc: ImageCaptureException) {
                    Toast.makeText(
                        requireActivity(),
                        "Failed to take a picture.",
                        Toast.LENGTH_SHORT
                    ).show()
                    Log.e(TAG, "onError: ${exc.message}")
                }
            }
        )
    }

这就是碎片位移的方法

fun showFragment(bundle: Bundle) {
        val saveFragment = SaveFragment()
        saveFragment.arguments = bundle

        val fragmentManager = childFragmentManager
        fragmentManager.beginTransaction().apply {
            replace(R.id.save_fragment, saveFragment)
            addToBackStack(null)
            commit()
        }
    }

我尝试更改 .xml 和导航中的 id。但我仍然遇到同样的错误。

<FrameLayout
    android:id="@+id/save_fragment"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.save.SaveFragment">
android android-fragments fragment
1个回答
0
投票

检查此 id/navigation_save 是否正确

© www.soinside.com 2019 - 2024. All rights reserved.