创建服务时没有空的构造函数

我纠结于这个错误:

08-0811:42:53.179: E/AndroidRuntime (20288) : 由 java.lang 引起。 InstantiationException: 不能实例化类 com.example.localnotificationtest。 ReminderService; 没有空构造函数

我不明白为什么会出现这种错误。

我试图出现在特定的时间和通知后,搜索的时间,我发现这个 旧的堆栈溢出问题。我试了所有的方法,但是我的代码出错了。

请帮我解决这个问题。

这是我的 MainActivity 代码:

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);
}
};


}

这是我的提醒服务代码:

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);
}


}

下面是我的 Manif.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>

我不知道哪里出错了,我是不是漏掉了什么密码?

51895 次浏览

You need to add an empty constructor to your class i.e. one that takes no arguments:

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

Explanation from the documentation:

The name is used to name the worker thread.

NOTE: this is only applicable to intent service.

You need to add the default no-argument constructor to your ReminderService class. This is only implicitly added if you don't write a constructor of your own (which you have). See here: http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

Declare a default no-argument constructor for IntentService

public class ReminderService extends IntentService {
public ReminderService() {
super("ReminderService");
}
}

If you have your Service declared as an Inner Class / Nested Class, you also need to make the class static

Without that you´ll get the error even if your constructor is correct

Explanation

The reason for that is, you can only instantiate inner classes in the context of the outer class, so you would need to create an instance of the outer class first.

Declaring your inner class static makes it independent from its outer class