12

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

我正在使用 IntentService 下载我的数据。我的 IntentService 类定义如下:

public class DownloadService extends IntentService{
super("DownloadService");
@Override
    protected void onHandleIntent(Intent intent) {
        //download tasks...
    }
}

AndroidManifest.xml:

<appilcation>
<service android:name="DownloadService"/>
..
..
..
</application>

LauncherActivity.java:

@Override
public void onCreate(Bundle savedInstanceState) {
        ..
        ..
        ..
    Intent dwnldService = new Intent(this, DownloadService.class);
    startService(dwnldService);
}

但我仍然收到此错误:

08-13 08:57:09.416: E/AndroidRuntime(942): FATAL EXCEPTION: main
08-13 08:57:09.416: E/AndroidRuntime(942): java.lang.RuntimeException: Unable to instantiate service com.app.android.DowloadService: java.lang.InstantiationException: can't instantiate class com.app.android.DowloadService; no empty constructor
08-13 08:57:09.416: E/AndroidRuntime(942):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:2237)
08-13 08:57:09.416: E/AndroidRuntime(942):  at android.app.ActivityThread.access$1600(ActivityThread.java:123)
08-13 08:57:09.416: E/AndroidRuntime(942):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
08-13 08:57:09.416: E/AndroidRuntime(942):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 08:57:09.416: E/AndroidRuntime(942):  at android.os.Looper.loop(Looper.java:137)
08-13 08:57:09.416: E/AndroidRuntime(942):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-13 08:57:09.416: E/AndroidRuntime(942):  at java.lang.reflect.Method.invokeNative(Native Method)
08-13 08:57:09.416: E/AndroidRuntime(942):  at java.lang.reflect.Method.invoke(Method.java:511)
08-13 08:57:09.416: E/AndroidRuntime(942):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-13 08:57:09.416: E/AndroidRuntime(942):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-13 08:57:09.416: E/AndroidRuntime(942):  at dalvik.system.NativeStart.main(Native Method)
08-13 08:57:09.416: E/AndroidRuntime(942): Caused by: java.lang.InstantiationException: can't instantiate class com.app.android.DowloadService; no empty constructor
08-13 08:57:09.416: E/AndroidRuntime(942):  at java.lang.Class.newInstanceImpl(Native Method)
08-13 08:57:09.416: E/AndroidRuntime(942):  at java.lang.Class.newInstance(Class.java:1319)
08-13 08:57:09.416: E/AndroidRuntime(942):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:2234)
4

2 回答 2

33
public DownloadService() {
    super("Download service");
}

尝试将此添加到您的服务中。

于 2012-08-13T09:19:24.247 回答
2

在您的类中添加带有 super("name") 调用的默认构造函数

public DownloadService() {
    super("DownloadService");
 }
于 2012-08-13T09:45:22.980 回答