如何添加在 popupWindow 中打开画廊的 onClickListener

How to add onClickListener which opens gallery inside popupWindow

所以我想在 popupWindow 中添加视频和图像选择,然后将所有这些添加到 recyclerView 中。但是当我尝试打开我的 popupWindow 应用程序崩溃时,我检查了我的 logcat 并且我明白为什么我的应用程序不起作用(我认为)但我不知道如何在我的代码中解决这个问题.

here 是没有任何功能的预览

我想用 'startActivityForResult' 来做这个,但是这个方法已经过时了,而且对我来说更复杂

MainActivity.kt

class MainActivity : AppCompatActivity() {

private val home = HomeFragment()
private val player = PlayerFragment()
private val profile = ProfileFragment()
private val settings = SettingsFragment()


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

    val popupButton: FloatingActionButton = findViewById(R.id.fab)
    val bottomNav: BottomNavigationView = findViewById(R.id.bottomNavigationView)

    bottomNav.background = null
    bottomNav.menu.findItem(R.id.placeholder).isEnabled = false
    replaceFragment(home)

    bottomNav.setOnItemSelectedListener {
        when (it.itemId) {
            R.id.home -> replaceFragment(home)
            R.id.player -> replaceFragment(player)
            R.id.profile -> replaceFragment(profile)
            R.id.settings -> replaceFragment(settings)
        }
        true
    }
    popupButton.setOnClickListener {
        showDialog()
    }

}
private fun replaceFragment(fragment: Fragment) {
    val transaction = supportFragmentManager.beginTransaction()
    transaction.replace(R.id.fragment_container, fragment)
    transaction.commit()
}

private fun showDialog() {

    val title = RecyclerAdapter().titles
    val description = RecyclerAdapter().details
    val video = RecyclerAdapter().videos

    val dialog = Dialog(this)
    val dialogView: View = layoutInflater.inflate(R.layout.popup, null)

    val imageView = dialogView.findViewById<ImageView>(R.id.imageChange)
    val videoView = dialogView.findViewById<ImageView>(R.id.VideoSelect)

    val titleEditText = dialogView.findViewById<EditText>(R.id.titleEdit) //popUp edit field title
    val descEditText = dialogView.findViewById<EditText>(R.id.description) //popUp pole edit field description

    val getPreviewImage = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
        imageView.setImageURI(it)
    })
    val getPreviewVideo = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
        videoView.setImageURI(it)
    })


    dialogView.addImage?.setOnClickListener {
        getPreviewImage.launch("image/*")
    }

    dialogView.addVideo?.setOnClickListener {
        getPreviewVideo.launch("video/*")
    }

    dialogView.addButton?.setOnClickListener {
        if (titleEditText.text.isEmpty()){
            Toast.makeText(applicationContext, "add required data", Toast.LENGTH_SHORT).show()
        }else{
            Toast.makeText(applicationContext, "Added", Toast.LENGTH_SHORT).show()
        }
    }
    dialog.setContentView(dialogView)
    dialog.show()
 }
}

调试控制台

E/AndroidRuntime: FATAL EXCEPTION: main
Process: eu.tuto.youtubeproject, PID: 15323
java.lang.IllegalStateException: LifecycleOwner eu.tuto.youtubeproject.MainActivity@4f4d646 is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.
    at androidx.activity.result.ActivityResultRegistry.register(ActivityResultRegistry.java:123)
    at androidx.activity.ComponentActivity.registerForActivityResult(ComponentActivity.java:682)
    at androidx.activity.ComponentActivity.registerForActivityResult(ComponentActivity.java:691)
    at eu.tuto.youtubeproject.MainActivity.showDialog(MainActivity.kt:85)
    at eu.tuto.youtubeproject.MainActivity.onCreate$lambda-1(MainActivity.kt:60)
    at eu.tuto.youtubeproject.MainActivity.$r8$lambda$pybEQF7uXWpGKgIZJas8o7AXY1Q(Unknown Source:0)
    at eu.tuto.youtubeproject.MainActivity$$ExternalSyntheticLambda3.onClick(Unknown Source:2)
    at android.view.View.performClick(View.java:7191)
    at android.view.View.performClickInternal(View.java:7164)
    at android.view.View.access00(View.java:821)
    at android.view.View$PerformClick.run(View.java:27856)
    at android.os.Handler.handleCallback(Handler.java:914)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7551)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)

调查docs。您应该在生命周期处于 START 状态之前调用 registerForActivityResult。调用registerForActivityResult初始化成员变量绝对合法

private val home = HomeFragment()
private val player = PlayerFragment()
private val profile = ProfileFragment()
private val settings = SettingsFragment()
val getPreviewImage = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
        // your logic 
    })
val getPreviewVideo = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
       // your logic 
    })

override fun onCreate(savedInstanceState: Bundle?)
...

好吧,现在在@Janush FM 的帮助下我弄明白了。 我将 registerForActivityResult 的变量向上移动

class MainActivity : AppCompatActivity() {

private val home = HomeFragment()
private val player = PlayerFragment()
private val profile = ProfileFragment()
private val settings = SettingsFragment()

private val getPreviewImage = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
    imageChange.setImageURI(it)
})
val getPreviewVideo = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
    VideoSelect.setImageURI(it)
})

其中 imageChangeVideoSelect 是我的 ImageViewPopupWindow 中的 ID,而 setonClickListenerShowDialog() 函数中 [=19] =]

private fun showDialog() {

    val title = RecyclerAdapter().titles
    val description = RecyclerAdapter().details
    val video = RecyclerAdapter().videos

    val dialog = Dialog(this)
    val dialogView: View = layoutInflater.inflate(R.layout.popup, null)

    val imageView = dialogView.findViewById<ImageView>(R.id.imageChange)
    val videoView = dialogView.findViewById<ImageView>(R.id.VideoSelect)

    val titleEditText = dialogView.findViewById<EditText>(R.id.titleEdit) //popUp edit field title
    val descEditText = dialogView.findViewById<EditText>(R.id.description) //popUp pole edit field description


    dialogView.addImage?.setOnClickListener {
        getPreviewImage.launch("image/*")
    }

    //dialogView.addVideo?.setOnClickListener {
        //getPreviewVideo.launch("video/*")
    //}

    dialogView.addButton?.setOnClickListener {
        if (titleEditText.text.isEmpty()){
            Toast.makeText(applicationContext, "add required data", Toast.LENGTH_SHORT).show()
        }else{
            Toast.makeText(applicationContext, "Added", Toast.LENGTH_SHORT).show()
        }
    }
    dialog.setContentView(dialogView)
    dialog.show()
}