Подтвердить что ты не робот

Ошибка IntentService: нет конструктора по умолчанию

Я новичок в Android, и я пытаюсь использовать IntentService, и ошибка заключается в отсутствии конструктора по умолчанию Я попытался перезапустить студию Android, она не работает.

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
    }

Ошибка журнала ошибок

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)

Скриншот файла AndroidManifest введите описание изображения здесь

4b9b3361

Ответ 1

Просто добавьте конструктор по умолчанию

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

Это означает, что конструктор без параметра

Ответ 2

См. руководство разработчика Android для создания фоновых служб https://developer.android.com/training/run-background-service/create-service.html

Из того, что я собираю, все, что вам нужно сделать, это создать службу намерения, создав класс, который расширяет IntentService и внутри него, определите метод, который переопределяет onHandleIntent().

Вам нужно удалить конструктор, который вы определили для своего класса DisplayNotification, или добавить конструктор по умолчанию:

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

Ответ 3

Кажется, что вам нужен конструктор без параметров, например:

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

Сообщите мне, если это поможет.