I am trying to create an app that reads sms messages and phone calls data so it could provide some information like how much sms/calls did I make in last 24 hours or who is the person that I am texting/talking to the most.
So far I tried to read sms messages using code below but my app keeps crashing.
package com.example.mobilestats
import android.net.Uri
import android.app.Application
class SmsData : Application() {
private var _smsBodyList = getSMS()
fun getTotalSmsCount(): Int {
return _smsBodyList.size
}
fun getSMS(): ArrayList<String> {
var sms = ArrayList<String>()
var smsURI = Uri.parse("content://sms/inbox")
var cur = applicationContext.contentResolver.query(smsURI, null, null, null, null)
while(cur != null && cur.moveToNext()) {
var body = cur.getString(cur.getColumnIndex("body"))
sms.add(body)
}
if(cur != null) {
cur.close()
}
return sms
}
}
I think the problem is when I try to make query but I don't get any error report, the application just crashes.
I enabled READ_SMS permission in manifest file.
<uses-permission android:name="android.permission.READ_SMS"/>
EDIT: I am providing logcat error message.
03-28 17:53:21.709 30686-30686/com.example.mobilestats E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mobilestats, PID: 30686
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mobilestats/com.example.mobilestats.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
at com.example.mobilestats.SmsData.getSMS(SmsData.kt:17)
at com.example.mobilestats.SmsData.<init>(SmsData.kt:7)
at com.example.mobilestats.MainActivity.onCreate(MainActivity.kt:17)
at android.app.Activity.performCreate(Activity.java:6309)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)