【问题标题】:Using contentResolver out of Mainactivity creates Null pointer exception getApplicationContext() on a null object reference在 Mainactivity 之外使用 contentResolver 在空对象引用上创建空指针异常 getApplicationContext()
【发布时间】:2020-09-07 17:51:40
【问题描述】:

我正在尝试 Mainactivity 之外的 ContentResolver 。有没有办法在没有空指针异常的情况下访问

ImageFragment.kt 扩展 Appcompatactivity()


Val uri=contentResolver.insert(MediaStore.images.Media.EXTERNAL_CONTENT_URI,value)

编辑

This line works perfectly inside Mainactivity. I there a way to access outside main class correctly  or it should be only inside Mainactivity

class ImageFragment(context:Context) :AppCompatActivity() {
    var isImageSaved: Boolean = true
    var rot=0
    val mainContext = context // updated code
    var lensFacing=CameraSelector.LENS_FACING_BACK
    fun savingImg(img: Image) {
        val ios: OutputStream?
        val bit: Bitmap?
        var rotBit: Bitmap?
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val value = ContentValues()
            val filename =
                SimpleDateFormat(
                    FILENAME_FORMAT,
                    Locale.US
                ).format(System.currentTimeMillis()) + ".jpg"
            value.put(MediaStore.Images.Media.TITLE, filename)
            value.put(MediaStore.Images.Media.DISPLAY_NAME, filename)
            value.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
            value.put(
                MediaStore.Images.Media.RELATIVE_PATH,
                Environment.DIRECTORY_DCIM + "/" + "Camcorder"
            )
            val uri = mainContext.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, value)!! //  Updated with main context
            ios =mainContext.contentResolver.openOutputStream(uri)!!
            bit = img.toBitmap()
            setThumbnail(uri)
}
    ios.close()
        img.close()
        isImageSaved = true
}
private fun Image.toBitmap():Bitmap{
        val yBuffer=planes[0].buffer
        val ySize=ByteArray(yBuffer.remaining())
        yBuffer.get(ySize)
        return BitmapFactory.decodeByteArray(ySize,0,ySize.size,null)

    }
    private fun setThumbnail(uri: Uri) {
        gallery.post {
            gallery.setPadding(4)
            gallery.load(uri) {
                crossfade(true)
                transformations(CircleCropTransformation())
            }
        }
    }
    private fun rotateBitmap(Bit: Bitmap, angle: Int): Bitmap {
        val newMat = Matrix()
        if (lensFacing == CameraSelector.LENS_FACING_FRONT) {
            newMat.preScale(1F, -1F)
        }
        newMat.postRotate(angle.toFloat())
        return Bitmap.createBitmap(Bit, 0, 0, Bit.width, Bit.height, newMat, true)

    }
}

MainActivity.kt

val obj=ImageFragment(this)

我尝试将 Main 上下文传递给此类,但不起作用 我不知道为什么会发生这种情况,因为 idle 向我显示了没有范围的内容解析器。

谢谢

【问题讨论】:

  • “这条线在 Mainactivity 中完美运行”——这似乎不是有效的 Kotlin 语法(val 需要小写)。即使固定,这条线在活动中的任何地方都不会有效。我建议您发布整个ImageFragment 课程,以便我们可以准确地看到您拥有此代码的位置。
  • 只要您有Context 参考,您就可以访问contentResolver。您可以轻松地将 context 引用传递给纯 java 类并使用您的逻辑。
  • 我尝试使用 Mainactivity 引用但同样的异常。在 op 中添加了图像片段

标签: android kotlin nullpointerexception android-contentresolver


【解决方案1】:

使用 MainActivity“上下文”访问另一个类中的 contentResolver。

如果你使用的是适配器类,那么

context.getContentResolver();

和片段

getActivity.getContentResolver();

【讨论】:

  • 我在这个类中使用主要活动上下文但同样的例外。更新下编辑代码
猜你喜欢
  • 2018-06-07
  • 1970-01-01
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
相关资源
最近更新 更多