簡體   English   中英

從android中的相同活動啟動服務

[英]Start service from the same activity in android

我有一個擴展Intent服務的類。 我想從這個活動開始服務,但我得到了錯誤:

    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)

如何從同一活動開始服務?

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

看起來你正在用testActivity調用startService,這是一個活動,而不是一個服務。

試試這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM