简体   繁体   中英

Start service from the same activity in android

I have a class which extend Intent service. 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.

try like that:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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