IntentService 错误:没有默认构造函数。

36

我是 Android 的新手,我正在尝试使用 IntentService,但是我收到一个错误消息,说它没有默认构造函数。我尝试重新启动 Android Studio,但不起作用。

package com.example.hitesh.kuchbhi;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.NotificationCompat;

import java.util.TreeSet;

public class DisplayNotification extends IntentService {
    public DisplayNotification(String name) {
        super("DisplayNotification");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // my code which even after deleting gives the same error
    }

日志cat错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.hitesh.kuchbhi, PID: 28899
                  java.lang.RuntimeException: Unable to instantiate service com.example.hitesh.kuchbhi.DisplayNotification: java.lang.InstantiationException: class com.example.hitesh.kuchbhi.DisplayNotification has no zero argument constructor
                      at android.app.ActivityThread.handleCreateService(ActivityThread.java:2728)
                      at android.app.ActivityThread.access$1800(ActivityThread.java:144)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5233)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
               

Android清单文件截图 在此输入图片描述


<!--suppress AndroidDomInspection --> | 不建议,但如果您想暂停警告 - exploitr
6个回答

114

只需添加默认构造函数

public DisplayNotification() {
    super("DisplayNotification");
}
这意味着一个没有参数的构造函数。

非常感谢您。我差点花了一整天的时间来完成这个! - jht
非常欢迎。如果这解决了您的问题,请将其标记为答案,以便其他遇到类似问题的人可以得到解决方案。 - Ratul Bin Tazul
2
请注意,这个构造函数必须是“public”。 - hampercm

4
请参考Android开发者指南创建后台服务,https://developer.android.com/training/run-background-service/create-service.html
据我了解,您只需要创建一个扩展IntentService的类,并在其中定义一个覆盖onHandleIntent()方法的方法即可。
您需要删除已定义的DisplayNotification类构造函数,或添加默认构造函数,例如:
public DisplayNotification() {
    super("DisplayNotification");
}

2

看起来你需要一个无参数构造函数,像这样:

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

如果有帮助,请告诉我。


0

只需添加另一个构造函数:

public DisplayNotification() {
    this("DisplayNotification");
}

0

@jht

另一种方式——通过参数化的参数提供默认构造函数**public IntentService(String name)**,其中String name用于命名工作线程,因此,

0

Intent Service需要一个参数的构造函数,因此我们需要提供没有参数的构造函数,并在其中使用parms super。

示例:

没有参数的构造函数,带有param super。

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

带有参数 super 的构造函数

public DisplayNotification(String name) {
    super(name);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接