Android-IllegalStateException:cursor.getString(idx)不得为null [英] Android - IllegalStateException : cursor.getString(idx) must not be null

查看:409
本文介绍了Android-IllegalStateException:cursor.getString(idx)不得为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经关注了这里的所有内容,但是为什么cursor.getString(idx)说它一定不能是null?我提供了所有必要的参数.

I have follow all the stuffs here, but Why does cursor.getString(idx) said it must not be null? I supply all the necessary params..

这是我的getfilepath:

fun getFilePathFromUri(context: Context, imageURI: Uri): String? {

        var cursor : Cursor? = null
        var result: String

        try {
            val projection = arrayOf(MediaStore.Images.Media.DATA)

            cursor = context.contentResolver.query(imageURI, projection, null, null, null)

            if (cursor == null) {
                result = imageURI.path
            } else {
                Log.d(AppConstants.TAG, "Path_img = " + imageURI)
                Log.d(AppConstants.TAG, "Path_pth = " + imageURI.path)
                Log.d(AppConstants.TAG, "Path_cursor = " + cursor)

                cursor.moveToFirst()
                val idx = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA)
                result = cursor.getString(idx)
            }
            return result
        } finally {
                cursor!!.close()
        }
    }

这是错误:

12-04 14:34:44.971 2008-2008/com.xxx.project k E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.xxx.proj, PID: 2008
    java.lang.IllegalStateException: cursor.getString(idx) must not be null
    at com.xxx.proj.utils.ImageUtils$Companion.getFilePathFromUri(ImageUtils.kt:78)
    at com.xxx.proj.api.Layer.createNewLayer(Layer.java:84)
    at com.xxx.proj.dialog.EventMapCreationDialog.registerNewMap(EventMapCreationDialog.kt:266)
    at com.xxx.proj.dialog.EventMapCreationDialog.onClick(EventMapCreationDialog.kt:82)
    at android.view.View.performClick(View.java:5204)
    at android.view.View$PerformClick.run(View.java:21153)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

推荐答案

每个 cursor.getString()的文档:

以字符串形式返回请求的列的值.

Returns the value of the requested column as a String.

结果以及该方法在列时是否引发异常 值是null或列类型不是字符串类型是 实现定义的.

The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined.

您得到一个IllegalStateException.尽管范围很广,但这通常意味着方法在错误/非法的时间被调用.也就是说,应用程序或环境对于此方法未处于正确的状态.这可能意味着参数,游标或值为空.

You're getting an IllegalStateException. Though broad, this generally means that a method has been invoked at a wrong/illegal time. That is, the the application or environment is not in the proper state for this method. This could mean that the parameter, cursor, or value is null.

在调用方法之前检查列的返回类型:

Check the return type of the column before calling the method:

if (cursor.getType(idx) == FIELD_TYPE_STRING) {
    result = cursor.getString(idx);
}

这篇关于Android-IllegalStateException:cursor.getString(idx)不得为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆