![](/img/trans.png)
[英]How can I start a service from an activity that is not in the same project in 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.