ไม่มีตัวสร้างว่างเมื่อสร้างบริการ


98

ฉันกำลังดิ้นรนกับข้อผิดพลาดนี้:

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>

ฉันไม่รู้ว่าฉันผิดพลาดตรงไหน ฉันไม่มีรหัสหรือไม่?

คำตอบ:


222

คุณต้องเพิ่มตัวสร้างว่างในคลาสของคุณนั่นคือสิ่งที่ไม่มีข้อโต้แย้ง:

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

คำอธิบายจากเอกสาร :

nameจะใช้ในการตั้งชื่อหัวข้อของผู้ปฏิบัติงาน

หมายเหตุ : ใช้ได้เฉพาะกับการบริการตามความตั้งใจเท่านั้น


9
ผู้สร้างที่กล้าหาญโดยไม่มีข้อโต้แย้ง
Pratik Butani

44

หากคุณมีบริการของคุณที่ประกาศเป็นคลาสชั้นใน / คลาสที่ซ้อนกันคุณจะต้องทำให้คลาสคง

หากไม่มีสิ่งนั้นคุณจะได้รับข้อผิดพลาดแม้ว่าตัวสร้างของคุณจะถูกต้องก็ตาม

คำอธิบาย

เหตุผลก็คือคุณสามารถสร้างอินสแตนซ์คลาสภายในได้ในบริบทของคลาสชั้นนอกเท่านั้นดังนั้นคุณจะต้องสร้างอินสแตนซ์ของคลาสชั้นนอกก่อน

การประกาศคลาสภายในของคุณคงทำให้เป็นอิสระจากคลาสนอก


เอกสารนี้อยู่ที่ไหน?
Ciro Santilli 郝海东冠状病六四事件法轮功

ฉันไม่รู้ว่ามีการจัดทำเอกสารไว้ที่ใด แต่ถ้าคุณต้องการสร้างอินสแตนซ์คลาสที่ไม่ใช่แบบคงที่ชั้นนอกจะต้องสร้างอินสแตนซ์ก่อน ตอนนี้ถ้าคุณเริ่มบริการอินสแตนซ์จะถูกสร้างขึ้นโดยระบบซึ่งจะไม่รู้ว่าต้องสร้างอินสแตนซ์จากคลาสภายนอกก่อนจึงจะเกิดความผิดพลาด แต่ข้อผิดพลาดนั้นทำให้เข้าใจผิดจริงๆในกรณีนั้น ดูที่stackoverflow.com/questions/70324/…
Marian Klühspies

32

ประกาศตัวสร้างไม่มีอาร์กิวเมนต์เริ่มต้นสำหรับIntentService

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

7

คุณต้องเพิ่มตัวสร้างไม่มีอาร์กิวเมนต์เริ่มต้นในคลาสReminderServiceของคุณ สิ่งนี้จะถูกเพิ่มโดยปริยายหากคุณไม่ได้เขียนตัวสร้างของคุณเอง (ซึ่งคุณมี) ดูที่นี่: http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.