简体   繁体   English

从android中的相同活动启动服务

[英]Start service from the same activity in android

I have a class which extend Intent service. 我有一个扩展Intent服务的类。 I want to start service from this activity but I get erroor: 我想从这个活动开始服务,但我得到了错误:

    Intent msgIntent = new Intent();
    msgIntent.setClass(testActivity, testActivity.class);
    startService(msgIntent);

07-15 11:53:33.030: E/AndroidRuntime(28989): java.lang.NullPointerException
07-15 11:53:33.030: E/AndroidRuntime(28989):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)

How can I start service from the same activity? 如何从同一活动开始服务?

Intent msgIntent = new Intent(YourClass.this, TestService.class);
startService(msgIntent);

it looks like you are calling startService with testActivity, which is an activity, not a service. 看起来你正在用testActivity调用startService,这是一个活动,而不是一个服务。

try like that: 试试这样:

Intent msgIntent = new Intent(this, yourIntentService.class);
startService(msgIntent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM