I'm new in android. I need to use MainActivity function in service to reach service's onstartcommand again
What I expect is first service will run because of on create method but because of action is not "ACTION_PLAY" service will run MainActivity.start() function then service start again and I get println("action play working")
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intent = Intent(this,myService::class.java)
intent.putExtra("action","ACTION_STOP")
startService(intent)
}
fun start(){
val intent = Intent(this,myService::class.java)
intent.putExtra("action","ACTION_PLAY")
startService(intent)
}
}
class myService: Service() {
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val action = intent?.getStringExtra("action")
if (action == "ACTION_PLAY"){
println("action play working")
}
else{
MainActivity().start()
}
return super.onStartCommand(intent, flags, startId)
}
}
But it gives me this error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.isililkproje, PID: 9526
java.lang.RuntimeException: Unable to start service com.example.isililkproje.myService@95959e6 with Intent { cmp=com.example.isililkproje/.myService (has extras) }: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3314)
at android.app.ActivityThread.-wrap21(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
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:106)
at com.example.isililkproje.MainActivity.start(MainActivity.kt:28)
at com.example.isililkproje.myService.onStartCommand(myService.kt:19)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3297)
at android.app.ActivityThread.-wrap21(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Please give me an explanation about this why it's giving error and how can I solve this thank you