提问人:dltayn 提问时间:7/18/2021 更新时间:7/18/2021 访问量:122
在服务中使用MainActivity函数会产生错误,为什么?
Using MainActivity function inside of service is giving error why?
问:
我是android的新手。我需要在服务中使用MainActivity函数才能再次访问服务的onstart命令
我期望的是第一个服务会因为 on create 方法而运行,但由于操作不是“ACTION_PLAY”服务将运行 MainActivity.start() 函数,然后服务再次启动,我得到 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)
}
}
但它给了我这个错误
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)
请给我解释一下为什么它会出错以及我该如何解决这个问题,谢谢
答: 暂无答案
评论
onStartCommand()
performStart()
performSomethingOtherThanStart()
myService
onStartCommand()
Intent