title: ContentResolver.query returning null (even though it never returns null)
body: <p>I created a <code>ContentProvider</code> for my app to be used by all processes in it (not externally). Here is how it looks and how it's being used:</p>
  
  <p><strong>MyProvider.kt</strong></p>
  
  <pre><code>class MyProvider : ContentProvider() {
  
      lateinit var appContext: Context
      private lateinit var sharedPreferences: SharedPreferences
  
      private val uriMatcher = UriMatcher(UriMatcher.NO_MATCH).apply {
          this.addURI(
              AUTHORITY,
              URI_IS_FEATURE_ENABLED.pathSegments.joinToString("/"),
              URI_CODE_IS_FEATURE_ENABLED
          )
      }
  
      override fun onCreate(): Boolean {
          appContext = context!!.applicationContext
  
          sharedPreferences = appContext.getSharedPreferences(MY_SP_NAME, Context.MODE_PRIVATE)
  
          return true
      }
  
      override fun query(
          uri: Uri,
          projection: Array&lt;String&gt;?,
          selection: String?,
          selectionArgs: Array&lt;String&gt;?,
          sortOrder: String?
      ): Cursor? {
          return when (uriMatcher.match(uri)) {
              URI_CODE_IS_FEATURE_ENABLED -&gt; {
                  createQueryCursorResult(
                      sharedPreferences.getBoolean(SPK_IS_FEATURE_ENABLED, false)
                  )
              }
              else -&gt; {
                  // this never happens ;)
                  throw java.lang.IllegalArgumentException("invalid query uri: $uri")
              }
          }
      }
  
      override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array&lt;String&gt;?): Int {
          // NOT USED :)
          return 0
      }
  
      override fun delete(uri: Uri, selection: String?, selectionArgs: Array&lt;String&gt;?): Int {
          // NOT USED :)
          return 0
      }
  
      override fun insert(uri: Uri, values: ContentValues?): Uri? {
          // NOT USED :)
          return null
      }
  
      private fun createQueryCursorResult(value: Boolean): Cursor {
          return MatrixCursor(arrayOf(COLUMN_VALUE)).apply {
              if (value) {
                  this.addRow(arrayOf(1))
              } else {
                  this.addRow(arrayOf(0))
              }
          }
      }
  
      companion object {
          const val AUTHORITY = "com.my.app.package.example"
  
          const val COLUMN_VALUE = "col_value"
          const val MY_SP_NAME = "my_sp_name"
  
          const val SPK_IS_FEATURE_ENABLED = "spk_is_feature_enabled"
  
          val URI_IS_FEATURE_ENABLED = Uri.Builder()
              .scheme(ContentResolver.SCHEME_CONTENT)
              .authority(AUTHORITY)
              .appendPath("is_feature_enabled")
              .build()!!
  
          private const val URI_CODE_IS_FEATURE_ENABLED = 1
      }
  }
  </code></pre>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;provider
      android:authorities="com.my.app.package.my_provider"
      android:name=".ContentProviders.MyProvider"
      android:process=":my_provider_process"
      android:exported="false" /&gt;
  </code></pre>
  
  <p><strong>UsingMyProvider.kt</strong></p>
  
  <pre><code>class UsingMyProvider {
  
      fun query(context: Context): Boolean {
          synchronized(this) {
              context.contentResolver.query(
                  MyProvider.URI_IS_FEATURE_ENABLED,
                  null,
                  null,
                  null,
                  null
              )!!.use {
                  it.moveToFirst()
  
                  return it.getInt(it.getColumnIndex(MyProvider.COLUMN_VALUE)) != 0
              }
          }
      }
  }
  </code></pre>
  
  <p>The problem is that for some reason that is unknown to me, sometimes <code>context.contentResolver.query(...)</code> returns <code>null</code>. It is mostly happening on Samsung Devices (89%), and Android 6.x (84%). Obs.: I do never return <code>null</code> on my Provider.</p>
  
  <p>Unfortunately I could not reproduce it locally, but Crashlytics has a considerable amount of that crash :/.</p>
  
  <p>What is wrong with my code? I could not figure it out.</p>