嗨,我是Android开发的新手。我正在使用Android Kotlin。我想检查内容提供者中是否有任何数据。这是我在mainactivity中尝试的示例代码
class MainActivity : AppCompatActivity() {
companion object {
val AUTHORITY = "com.wsample.moblity.auth"
val CONTENT_URI = Uri.parse("content://$AUTHORITY")
}
var c:Cursor? = contentResolver.query(CONTENT_URI, null, null, null, null)
override fun onCreate(savedInstanceState: Bundle?) {
if (c!!.count != 0) {
Log.d("COUNT", "NOT ZERO")
} else {
Log.d("COUNT 1", "ZERO")
}
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
但是我遇到了类似的异常情况,
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
请帮助我。
答案 0 :(得分:2)
将此行移动到onCreate函数中
var c:光标? = contentResolver.query(CONTENT_URI,null,null,null,null)