这是我的代码
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
val cursor = getContentResolver().query(
CourseCatalog.Course.CONTENT_URI,
arrayOf(CourseCatalog.Course.PREFIX, CourseCatalog.Course.NUMBER, CourseCatalogContract.CourseListing.COLUMN_NAME_DESCRIPTION),
"prefix = ? OR prefix = ?",
arrayOf("CIS", "ECE"),
"",
null
)}
这是logcat
Process: edu.ksu.cs.coursecatalog, PID: 4845
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{edu.ksu.cs.coursecatalog/edu.ksu.cs.coursecatalog.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:99)
at edu.ksu.cs.coursecatalog.MainActivity.<init>(MainActivity.kt:15)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我在调试器中花费了3个小时,试图找出问题所在。我的工作目录中有内容提供程序,我不确定该怎么做。