Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntentService Error: No default Constructor

Tags:

android

I am a newbie to Android, and I am trying to use the IntentService but I get an error that it has no default constructor. I tried restarting the Android Studio it doesn't work.

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     } 

log cat 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 file Screenshot enter image description here

like image 734
jht Avatar asked Mar 30 '17 21:03

jht


People also ask

Why IntentService is deprecated?

This class was deprecated in API level 30. IntentService is subject to all the background execution limits imposed with Android 8.0 (API level 26). Consider using WorkManager or JobIntentService , which uses jobs instead of services when running on Android 8.0 or higher.

How do I start IntentService?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view, when user gets the data from intent service it will update.

Does IntentService run on background thread?

IntentService runs on its own thread. It will stop itself when it's done.


1 Answers

Just add a default constructor

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

That means a constructor with no parameter

like image 109
Ratul Bin Tazul Avatar answered Sep 29 '22 20:09

Ratul Bin Tazul