There is no empty constructor when creating the service

I am struggling with this error:

08-08 11: 42: 53.179: E / AndroidRuntime (20288): caused by: java.lang.InstantiationException: cannot create an instance of class com.example.localnotificationtest.ReminderService; no empty constructor

I do not understand why this error occurs.

I am trying to show a notification at a specific time and after searching for some time when I found this old stack question. I tried everything, but my code gives an error.

Please help me solve this problem.

Here is my MainActivity code:

public class MainActivity extends Activity { int mHour, mMinute; ReminderService reminderService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); reminderService = new ReminderService("ReminderService"); TimePickerDialog dialog = new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false); dialog.show(); } TimePickerDialog.OnTimeSetListener mTimeSetListener = new OnTimeSetListener() { @Override public void onTimeSet(TimePicker v, int hourOfDay, int minute) { mHour = hourOfDay; mMinute = minute; AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, Calendar.YEAR); c.set(Calendar.MONTH, Calendar.MONTH); c.set(Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH); c.set(Calendar.HOUR_OF_DAY, mHour); c.set(Calendar.MINUTE, mMinute); c.set(Calendar.SECOND, 0); long timeInMills = c.getTimeInMillis(); Intent intent = new Intent(MainActivity.this, ReminderService.class); PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0, intent, 0); alarmManager.set(AlarmManager.RTC, timeInMills, pendingIntent); } }; } 

and here is my ReminderService code:

 public class ReminderService extends IntentService { public ReminderService(String name) { super(name); // TODO Auto-generated constructor stub } @Override protected void onHandleIntent(Intent intent) { Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 1, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(this); builder.setContentIntent(contentIntent) .setSmallIcon(R.drawable.ic_launcher) .setTicker("Local Notification Ticker") .setWhen(System.currentTimeMillis()) .setAutoCancel(true) .setContentTitle("Local Notification") .setContentText("This is content text."); Notification n = builder.getNotification(); nm.notify(1, n); } } 

and here is my manifest.xml:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.localnotificationtest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="ReminderService"></service> </application> </manifest> 

I do not know where I am wrong. Am I missing some code?

+84
java android
Aug 08 2018-12-12T00:
source share
4 answers

You need to add an empty constructor to the ie class which takes no arguments:

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

Explanation from the documentation :

name used to indicate workflow.

NOTE : this applies only to the intent service.

+205
Aug 08 '12 at 7:20
source share

If your service is declared as an Inner class / Nested class , you also need to make the class static

Without this, you will get an error, even if your constructor is right.

+34
Feb 16 '15 at 0:51
source share

Declare a default constructor constructor for IntentService

 public class ReminderService extends IntentService { public ReminderService() { super("ReminderService"); } } 
+31
Aug 08 '12 at 7:20
source share

You need to add the default no-argument constructor to the ReminderService class. This is implicitly added if you are not writing your own constructor (which you have). See here: http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

+7
Aug 08 '12 at 7:20
source share



All Articles