100

我正在努力解决这个错误:

08-08 11:42:53.179:E/AndroidRuntime(20288):原因:java.lang.InstantiationException:无法实例化类 com.example.localnotificationtest.ReminderService;没有空的构造函数

我不明白为什么会发生此错误。

我试图在特定时间出现通知,在搜索了一段时间后,我发现了这个旧的 stackoverflow 问题。我尝试了一切,但我的代码给出了错误。

请帮我解决这个问题。

这是我的 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);
        }
    };

}

这是我的 ReminderService 代码:

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

}

这是我的 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>

我不知道我哪里错了。我错过了一些代码吗?

4

4 回答 4

224

您需要向您的类添加一个空的构造函数,即一个不带参数的构造函数:

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

文档中的解释:

name用于命名工作线程。

注意:这仅适用于意图服务。

于 2012-08-08T07:20:02.977 回答
44

如果您将 Service 声明为Inner Class / Nested Class,则还需要将类设为静态

没有它,即使你的构造函数是正确的,你也会得到错误

解释

原因是,您只能在外部类的上下文中实例化内部类,因此您需要先创建外部类的实例。

声明你的内部类静态使它独立于它的外部类

于 2015-02-16T00:51:38.437 回答
32

IntentService声明一个默认的无参数构造函数

public class ReminderService extends IntentService {
    public ReminderService() {
      super("ReminderService");
    }
}
于 2012-08-08T07:20:59.873 回答
7

您需要将默认的无参数构造函数添加到ReminderService类。如果您不编写自己的构造函数(您拥有),这只会隐式添加。见这里:http ://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

于 2012-08-08T07:20:16.270 回答