选择一个文件时,文件选择器的意图导致应用程序崩溃

时间:2018-08-12 03:14:03

标签: android android-intent kotlin onactivityresult

顾名思义,我的意图文件选择器出现了一个奇怪的错误。 我的意图是允许我选择多个文件。当我选择多个文件时,该应用程序不会崩溃。都很好。我可以播放所有选定的文件。但是,当我选择一个文件时,它由于一个奇怪的异常而崩溃(可能为空数据?)。 看,我实现了一个Songlist(这是Uri类型的可变列表)。它应该收到一个由意图返回的Uri并添加到其中。 预先感谢。

意图调用和ActivityResult的代码如下。

IntentCalling

fun onMusicFileChooserClicked(v: View) {
        val intent = Intent()
                .setType("audio/*")
                .setAction(Intent.ACTION_GET_CONTENT)
                .putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)

        startActivityForResult(Intent.createChooser(intent, "Select a file"), 111)
    }

ActivityResult

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 111 && resultCode == RESULT_OK) {
            for (i in 0 until data?.clipData!!.itemCount) {
                val uri = data?.clipData?.getItemAt(i)?.uri
                songlist.add(uri)

            }

            selectfilesucess = true
            updateMetaData()
        }
    }

日志

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: kalves.multimediacenter, PID: 19399
                  java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=111, result=-1, data=Intent { dat=content://com.android.externalstorage.documents/document/86BD-1E10:DCIM/Camera/03. Do What I Gotta Do.mp3 flg=0x1 launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } }} to activity {kalves.multimediacenter/kalves.multimediacenter.MainActivity}: kotlin.KotlinNullPointerException
                      at android.app.ActivityThread.deliverResults(ActivityThread.java:4560)
                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:4603)
                      at android.app.ActivityThread.-wrap22(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1702)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6836)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
                   Caused by: kotlin.KotlinNullPointerException
                      at kalves.multimediacenter.MainActivity.onActivityResult(MainActivity.kt:179)
                      at android.app.Activity.dispatchActivityResult(Activity.java:7274)
                      at android.app.ActivityThread.deliverResults(ActivityThread.java:4556)
                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:4603) 
                      at android.app.ActivityThread.-wrap22(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1702) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6836) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

1 个答案:

答案 0 :(得分:0)

解决了。

问题是我没有检查空指针。基本上,我必须检查clipdate == null,如果为true,则它是单个文件。如果不是,则为多个文件。 傻我希望这对某人有帮助。

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 111 && resultCode == RESULT_OK && data!=null) {
            val clipdata = data.clipData
            if(clipdata!=null){
                for (i in 0 until data?.clipData!!.itemCount) {
                    val uri = data?.clipData?.getItemAt(i)?.uri
                    songlist.add(uri)

                }
            }else{
                val uri = data?.data
                songlist.add(uri)
            }

        }