सेवा बनाते समय कोई खाली कंस्ट्रक्टर नहीं


98

मैं इस त्रुटि से जूझ रहा हूं:

08-08 11: 42: 53.179: E / AndroidRuntime (20288): इसके कारण: java.lang.InstantiationException: क्लास कोम नहीं कर सकते। कोई खाली कंस्ट्रक्टर नहीं

मुझे समझ नहीं आता कि यह त्रुटि क्यों होती है।

मैं विशिष्ट समय पर अधिसूचना प्रकट करने का प्रयास कर रहा हूं और एक समय के लिए खोज करने के बाद मुझे यह पुराना स्टैकओवरफ्लो प्रश्न मिला । मैंने सब कुछ आजमाया लेकिन मेरा कोड त्रुटि देता है।

इस समस्या को हल करने में कृपया मेरी मदद करें।

यहाँ मेरा मुख्य कोड है:

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

}

और यहाँ मेरा मैनिफ़ेस्ट है। 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>

मुझे नहीं पता कि मैं कहां गलत हूं। क्या मुझे कुछ कोड याद आ रहे हैं?

जवाबों:


221

आपको अपनी कक्षा में एक खाली कंस्ट्रक्टर जोड़ने की आवश्यकता है, जो कि कोई तर्क नहीं लेता है:

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

प्रलेखन से स्पष्टीकरण :

nameकार्यकर्ता धागा नाम के लिए प्रयोग किया जाता है।

नोट : यह केवल इरादा सेवा के लिए लागू है।


9
बोल्ड द कन्स्ट्रक्टर विद नो
लॉजिक

44

यदि आपके पास आपकी सेवा को इनर क्लास / नेस्टेड क्लास के रूप में घोषित किया गया है , तो आपको कक्षा को स्थिर बनाने की भी आवश्यकता है

उसके बिना आपको त्रुटि मिलेगी भले ही आपका निर्माता सही हो

व्याख्या

इसका कारण यह है कि, आप बाहरी कक्षाओं के संदर्भ में केवल आंतरिक कक्षाओं को ही त्वरित कर सकते हैं, इसलिए आपको पहले बाहरी वर्ग का एक उदाहरण बनाने की आवश्यकता होगी।

अपने भीतर के वर्ग को स्थिर घोषित करना इसे अपने बाहरी वर्ग से स्वतंत्र बनाता है


यह कहां प्रलेखित है?
सिरो सेंटिल्ली 郝海东 冠状 iro i 法轮功 '

मैं नहीं जानता कि क्या इसे कहीं भी प्रलेखित किया गया है, लेकिन यदि आप आंतरिक गैर-स्थिर कक्षाओं को तत्काल बनाना चाहते हैं, तो बाहरी वर्ग को तुरंत पहले ही भेज दिया जाना चाहिए। अब यदि आप एक सेवा शुरू करते हैं, तो सिस्टम द्वारा उदाहरण बनाया जाता है, जो यह नहीं जानता कि उसे पहले बाहरी वर्ग से एक उदाहरण बनाना होगा, इसलिए यह दुर्घटनाग्रस्त हो जाएगा। लेकिन उस मामले में त्रुटि वास्तव में भ्रामक है। stackoverflow.com/questions/70324/…
Marian Klühspies

32

डिफॉल्ट नो-लॉजिक कंस्ट्रक्टर घोषित करें के लिए IntentService

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

7

आपको अपने अनुस्मारक अनुस्मारक वर्ग में डिफ़ॉल्ट नो-तर्क कंस्ट्रक्टर को जोड़ना होगा । यह केवल तभी जोड़ा जाता है यदि आप अपने स्वयं के एक निर्माता को नहीं लिखते हैं (जो आपके पास है)। यहां देखें: http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.