mol8
mol8

Reputation: 77

java.lang.RuntimeException: Unable to instantiate service {....} can't instantiate class; no empty constructor

I am having an error when trying to start a Service in my Android APP I start the service rigth before pausing the Activity, so I can upload some info to the server.

    java.lang.RuntimeException: Unable to instantiate service  can't instantiate class ; no empty constructor

The problem comes when I start the service:

 protected void onPause() {
    super.onPause();

    EditText nicknameText = (EditText) findViewById(R.id.EditText_Nickname);
    EditText emailText = (EditText) findViewById(R.id.EditText_Email);

    String strNickname = nicknameText.getText().toString();
    String strEmail = emailText.getText().toString();

    SharedPreferences.Editor editor = mGameSettings.edit();
    editor.putString(GAME_PREFERENCES_NICKNAME, strNickname);
    editor.putString(GAME_PREFERENCES_EMAIL, strEmail);

    editor.commit();

    //Antes de salir lanzamos una actividad nueva para hacer el upload de la info

    Intent uploadService = new Intent(getApplicationContext(),UploaderService.class);
    startService(uploadService);


}

The Service will then start an AsyncTask

public  class UploaderService extends Service{

    public UploaderService(){
        super();
    }

    private UpLoadUserData upLoadUserData;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        upLoadUserData = new UpLoadUserData();
        upLoadUserData.execute();
        Log.d(DEBUG_TAG, "Settings and image upload requested");
        return START_REDELIVER_INTENT;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // no binding
        return null;
    }
}

The manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidbook.btdt.hour6"
    android:versionCode="1"
    android:versionName="1.0"
    android:installLocation="auto">
    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="10"></uses-sdk>

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:debuggable="true">
        <activity
            android:name=".QuizSplashActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action
                    android:name="android.intent.action.MAIN" />
                <category
                    android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="QuizHelpActivity"></activity>
        <activity
            android:name="QuizMenuActivity"></activity>
        <activity
            android:name="QuizScoresActivity"></activity>
        <activity
            android:name="QuizSettingsActivity"></activity>
        <activity
            android:name="QuizGameActivity"></activity>
        <service android:name="QuizSettingsActivity$UploaderService"></service>
    </application>
</manifest>

Upvotes: 2

Views: 1603

Answers (2)

mol8
mol8

Reputation: 77

Thanks a lot!

I could successefully satart the Service by extending it from IntentService insetead of Service

  public static  class UploaderService extends IntentService{

    String DEBUG_TAG = UploaderService.class.getSimpleName();

    SharedPreferences mGameSettings;

and defining the Intent Service in the Manifest.xml

<service android:name=".QuizSettingsActivity$UploaderService"/>

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006674

First, you cannot have a Service that is an ordinary nested class. It would have to be a static nested class.

Second, having a Service that is a static nested class of an Activity is very strange.

Third, having a Service spawning an AsyncTask, rather than just using IntentService, is very strange.

Fourth, replace getApplicationContext() with this. Only use getApplicationContext() when you know why you are using getApplicationContext(), and you do not need it here.

Upvotes: 2

Related Questions