IntentService运行时异常

发布于 2024-12-21 10:14:07 字数 2811 浏览 1 评论 0原文

可能的重复:
Android RuntimeException:无法实例化服务

问题:启动 IntentService 生成 RuntimeException

我一无所知。如果您需要更多信息,请询问。日志:

D/dalvikvm( 6839): newInstance failed: no <init>()
D/AndroidRuntime( 6839): Shutting down VM
W/dalvikvm( 6839): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 6839): FATAL EXCEPTION: main
E/AndroidRuntime( 6839): java.lang.RuntimeException: Unable to instantiate service org.xyz.android.service.MyService: java.lang.InstantiationException: org.xyz.android.service.MyService
E/AndroidRuntime( 6839):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1929)
E/AndroidRuntime( 6839):    at android.app.ActivityThread.access$2500(ActivityThread.java:117)
E/AndroidRuntime( 6839):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
E/AndroidRuntime( 6839):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 6839):    at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 6839):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 6839):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 6839):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 6839):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 6839):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 6839):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 6839): Caused by: java.lang.InstantiationException: org.xyz.android.service.MyService
E/AndroidRuntime( 6839):    at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 6839):    at java.lang.Class.newInstance(Class.java:1409)
E/AndroidRuntime( 6839):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1926)
E/AndroidRuntime( 6839):    ... 10 more

此代码调用服务(它是从 ContentProvider 调用的):

Intent i = new Intent(
        getContext(),
        MyService.class);
    i.putStringArrayListExtra(EXTRA_MISSING, missingMonths);
    i.putExtra(EXTRA_URI, uri);
    i.putExtra(EXTRA_START, startTime);
    i.putExtra(EXTRA_END, endTime);
    getContext().startService(i);

这是与 IntentService 相关的代码块(只有两个方法。除了 asynctask 本身之外,当然):

public MyService(String name) {
    super("MyService");
}

@Override
protected void onHandleIntent(Intent intent) {
        // ... do stuff
        new DownloaderTask().execute(missingMonths);
}

Possible Duplicate:
Android RuntimeException: Unable to instantiate the service

Problem: starting an IntentService generates a RuntimeException.

I'm clueless. If you need more info, just ask. Log:

D/dalvikvm( 6839): newInstance failed: no <init>()
D/AndroidRuntime( 6839): Shutting down VM
W/dalvikvm( 6839): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 6839): FATAL EXCEPTION: main
E/AndroidRuntime( 6839): java.lang.RuntimeException: Unable to instantiate service org.xyz.android.service.MyService: java.lang.InstantiationException: org.xyz.android.service.MyService
E/AndroidRuntime( 6839):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1929)
E/AndroidRuntime( 6839):    at android.app.ActivityThread.access$2500(ActivityThread.java:117)
E/AndroidRuntime( 6839):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
E/AndroidRuntime( 6839):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 6839):    at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 6839):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 6839):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 6839):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 6839):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 6839):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 6839):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 6839): Caused by: java.lang.InstantiationException: org.xyz.android.service.MyService
E/AndroidRuntime( 6839):    at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 6839):    at java.lang.Class.newInstance(Class.java:1409)
E/AndroidRuntime( 6839):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1926)
E/AndroidRuntime( 6839):    ... 10 more

This code calls the service (it's being called from a ContentProvider):

Intent i = new Intent(
        getContext(),
        MyService.class);
    i.putStringArrayListExtra(EXTRA_MISSING, missingMonths);
    i.putExtra(EXTRA_URI, uri);
    i.putExtra(EXTRA_START, startTime);
    i.putExtra(EXTRA_END, endTime);
    getContext().startService(i);

And this is the IntentService relevant code blocks (there are only two methods. Besides the asynctask itself, of course):

public MyService(String name) {
    super("MyService");
}

@Override
protected void onHandleIntent(Intent intent) {
        // ... do stuff
        new DownloaderTask().execute(missingMonths);
}

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

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

发布评论

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

评论(1

ヅ她的身影、若隐若现 2024-12-28 10:14:07

您似乎缺少零参数公共构造函数。从当前构造函数中删除String name(因为您无论如何都没有使用它),然后就可以开始了。

You appear to be missing the zero-argument public constructor. Delete the String name from your current constructor (since you're not using it anyway), and you should be good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文