无法启动服务意图; Activity 内的 IntentService

发布于 2024-12-23 00:28:35 字数 3281 浏览 2 评论 0原文

我在活动中有这样一个服务:

public class StudyHalls extends ExpandableListActivity 
implements OnChildClickListener {

@Override 
public void onCreate(Bundle savedInstanceState) {
    ...
}

@Override
public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
        ...
}

public class RefreshService extends IntentService {

    public RefreshService() {
        super("RefreshService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        ...
    }

}
}

该服务将通过以下方式在另一个活动中启动:

   startService(new Intent(this, StudyHalls.RefreshService.class));

最后,我将服务添加到清单中:

     <service
        android:name="com.ecnu.sei.manuzhang.study.StudyHalls.RefreshService" />

这是错误信息:

    12-26 10:11:14.093: W/ActivityManager(70): Unable to start service Intent { cmp=com.ecnu.sei.manuzhang.study/.StudyHalls$RefreshService }: not found

编辑:感谢汤普森,我对清单进行了更改:

 <service
        android:name="com.ecnu.sei.manuzhang.study.StudyHalls$RefreshService" />

但是我仍然无法启动服务并出现错误:

    12-26 10:44:47.943: E/AndroidRuntime(782): FATAL EXCEPTION: main
    12-26 10:44:47.943: E/AndroidRuntime(782): java.lang.RuntimeException: Unable to instantiate service com.ecnu.sei.manuzhang.study.StudyHalls$RefreshService: java.lang.InstantiationException: com.ecnu.sei.manuzhang.study.StudyHalls$RefreshService
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1929) 
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.app.ActivityThread.access$2500(ActivityThread.java:117)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.os.Handler.dispatchMessage(Handler.java:99)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.os.Looper.loop(Looper.java:130)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at java.lang.reflect.Method.invokeNative(Native Method)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at java.lang.reflect.Method.invoke(Method.java:507)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at dalvik.system.NativeStart.main(Native Method)
    12-26 10:44:47.943: E/AndroidRuntime(782): Caused by: java.lang.InstantiationException: com.ecnu.sei.manuzhang.study.StudyHalls$RefreshService
    12-26 10:44:47.943: E/AndroidRuntime(782):  at java.lang.Class.newInstanceImpl(Native Method)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at java.lang.Class.newInstance(Class.java:1409)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1926)
    12-26 10:44:47.943: E/AndroidRuntime(782):  ... 10 more

I have such a Service inside an Activity as:

public class StudyHalls extends ExpandableListActivity 
implements OnChildClickListener {

@Override 
public void onCreate(Bundle savedInstanceState) {
    ...
}

@Override
public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
        ...
}

public class RefreshService extends IntentService {

    public RefreshService() {
        super("RefreshService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        ...
    }

}
}

The Service will be started in another activity through:

   startService(new Intent(this, StudyHalls.RefreshService.class));

Lastly, I add the Service into manifest:

     <service
        android:name="com.ecnu.sei.manuzhang.study.StudyHalls.RefreshService" />

Here's the error info:

    12-26 10:11:14.093: W/ActivityManager(70): Unable to start service Intent { cmp=com.ecnu.sei.manuzhang.study/.StudyHalls$RefreshService }: not found

EDIT: thx to Thompson, I've made a change to manifest:

 <service
        android:name="com.ecnu.sei.manuzhang.study.StudyHalls$RefreshService" />

but I still can't start service with the error:

    12-26 10:44:47.943: E/AndroidRuntime(782): FATAL EXCEPTION: main
    12-26 10:44:47.943: E/AndroidRuntime(782): java.lang.RuntimeException: Unable to instantiate service com.ecnu.sei.manuzhang.study.StudyHalls$RefreshService: java.lang.InstantiationException: com.ecnu.sei.manuzhang.study.StudyHalls$RefreshService
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1929) 
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.app.ActivityThread.access$2500(ActivityThread.java:117)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.os.Handler.dispatchMessage(Handler.java:99)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.os.Looper.loop(Looper.java:130)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at java.lang.reflect.Method.invokeNative(Native Method)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at java.lang.reflect.Method.invoke(Method.java:507)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at dalvik.system.NativeStart.main(Native Method)
    12-26 10:44:47.943: E/AndroidRuntime(782): Caused by: java.lang.InstantiationException: com.ecnu.sei.manuzhang.study.StudyHalls$RefreshService
    12-26 10:44:47.943: E/AndroidRuntime(782):  at java.lang.Class.newInstanceImpl(Native Method)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at java.lang.Class.newInstance(Class.java:1409)
    12-26 10:44:47.943: E/AndroidRuntime(782):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1926)
    12-26 10:44:47.943: E/AndroidRuntime(782):  ... 10 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人海汹涌 2024-12-30 00:28:35

这是一个内部类。您需要将清单更改为:

<service
    android:name="com.ecnu.sei.manuzhang.study.StudyHalls$RefreshService" />

It's an inner class. You need to change the manifest to this:

<service
    android:name="com.ecnu.sei.manuzhang.study.StudyHalls$RefreshService" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文