1

If someone could help with this problem I would very much appreciate it.

I have an activity starting a service using AlarmManager and it works fine. I created a new service (a modified copy) and this is giving the error: java.lang.RuntimeException: Unable to start service...with Intent {...(has extras) }: java.lang.ClassCastException

I've done a lot of searching and have checked my manifest many times as it seems to be the usual problem. Both services are declared, they are inside the application tags and are fully scoped. Most examples I've seen only refer to one service so, just in case, I've tried several different ways to code the 2 services in the manifest but have found no valid version other than the one below.

I've tried several ways to create the intent as I assume the problem lies in the fact that the new service is in a different package (within the same app).

I also tried an intent filter and custom action (see bottom) but it gave the same error.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="couk.jit.currencycheck1"
android:versionCode="1"
android:versionName="1.0.1" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="couk.jit.currencycheck1.MainMenu"
...        
</activity>

    <service
        android:name="couk.jit.currencycheck1.ServiceClass"
        android:enabled="true" />
    <service
        android:name="couk.jit.currencycheck1.xml.ServiceClassXML"
        android:enabled="true" />

    <activity
        android:name="couk.jit.currencycheck1.HistoryActivity"
        android:label="@string/title_activity_history" >
    </activity>
</application>
</manifest>

This version of the activity code gives the error.

import couk.jit.currencycheck1.xml.ServiceClassXML;
...
Intent intent;
if (dataSource == 0 || dataSource == 1)
intent = new Intent(this, ServiceClass.class);
else {
intent = new Intent(this, ServiceClassXML.class);   //gives error
}
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
scheduleStart.getTimeInMillis(), 60 * 1000, pintent);

These 2 versions do not give the error - but the service isn't reached. No log message, no action. (a)

//intent = new Intent(this, ServiceClassXML.class); //gives error
intent = new Intent("couk.jit.currencycheck1.xml.ServiceClassXML"); 

(b)

//intent = new Intent(this, ServiceClassXML.class); //gives error
//intent = new Intent("couk.jit.currencycheck1.xml.ServiceClassXML");
intent = new Intent();
intent.setComponent(new    ComponentName("couk.jit.currencycheck1.xml","ServiceClassXML.java"));

Also tried this, which gives same error.

intent = new Intent(context, ServiceClassXML.class);

Service class

public class ServiceClassXML extends Service {
SharedPreferences prefs = null;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("DBG", "ServiceClassXML started");
    prefs =   PreferenceManager.getDefaultSharedPreferences(this.getApplication().getApplicationContext());
    boolean run = prefs.getBoolean("running", false);
    if (!run)
        stopSelf();
    else {
        new GetXMLTask(this.getApplication().getApplicationContext(),  true).execute("");
    }
    return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
}

Attempt with intent filter and custom action. Manifest

    <service
        android:name="couk.jit.currencycheck1.xml.ServiceClassXML"
        android:enabled="true" >
        <intent-filter>
        <action android:name="couk.jit.currencycheck1.xml.DO_CUSTOM_ACTION"/>
        </intent-filter>
    </service>

Activity

//intent = new Intent(this, ServiceClassXML.class); 
//intent = new Intent("couk.jit.currencycheck1.xml.ServiceClassXML");
//intent = new Intent();
//intent.setComponent(new  ComponentName("couk.jit.currencycheck1.xml","ServiceClassXML.java")); //service not reached
intent = new Intent("couk.jit.currencycheck1.xml.DO_CUSTOM_ACTION");
intent.setClass(this, ServiceClassXML.class);

Logcat

09-03 16:56:32.344: E/AndroidRuntime(763): FATAL EXCEPTION: main
09-03 16:56:32.344: E/AndroidRuntime(763): java.lang.RuntimeException: Unable to start  service couk.jit.currencycheck1.xml.ServiceClassXML@4602fc38 with Intent { flg=0x4 cmp=couk.jit.currencycheck1/.xml.ServiceClassXML (has extras) }: java.lang.ClassCastException: android.app.Application
09-03 16:56:32.344: E/AndroidRuntime(763):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
09-03 16:56:32.344: E/AndroidRuntime(763):  at android.app.ActivityThread.access$3600(ActivityThread.java:125)
09-03 16:56:32.344: E/AndroidRuntime(763):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
09-03 16:56:32.344: E/AndroidRuntime(763):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 16:56:32.344: E/AndroidRuntime(763):  at android.os.Looper.loop(Looper.java:123)
09-03 16:56:32.344: E/AndroidRuntime(763):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-03 16:56:32.344: E/AndroidRuntime(763):  at java.lang.reflect.Method.invokeNative(Native Method)
09-03 16:56:32.344: E/AndroidRuntime(763):  at java.lang.reflect.Method.invoke(Method.java:521)
09-03 16:56:32.344: E/AndroidRuntime(763):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-03 16:56:32.344: E/AndroidRuntime(763):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-03 16:56:32.344: E/AndroidRuntime(763):  at dalvik.system.NativeStart.main(Native Method)
09-03 16:56:32.344: E/AndroidRuntime(763): Caused by: java.lang.ClassCastException: android.app.Application
09-03 16:56:32.344: E/AndroidRuntime(763):  at couk.jit.currencycheck1.xml.GetXMLTask.<init>(GetXMLTask.java:52)
09-03 16:56:32.344: E/AndroidRuntime(763):  at couk.jit.currencycheck1.xml.ServiceClassXML.onStartCommand(ServiceClassXML.java:24)
09-03 16:56:32.344: E/AndroidRuntime(763):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
09-03 16:56:32.344: E/AndroidRuntime(763):  ... 10 more
David Walton
  • 283
  • 1
  • 4
  • 12

1 Answers1

0

Bit the bullet and merged the packages into 1 - and still got the error! I know the line causing the problem (though don't yet know why), but I'm closing this question as it isn't the right question and I don't want to waste people's time.

David Walton
  • 283
  • 1
  • 4
  • 12