Wise
Wise

Reputation: 658

java.lang.RuntimeException: Unable to instantiate service

I am trying to code an application that monitors sms messages. I want to start a service from my main Activity class, but the service doesn't start for some reason. I think there might be a problem with how I declared my service in the manifest file or how I call it from my activity. Here is a part of my activity Code:

public class TablighBlockActivity extends Activity {
    /** Called when the activity is first created. */
    private ToggleButton toggEnable;

    public void onCreate(Bundle savedInstanceState) {
        if (loadState("running")==false){startService(new Intent(this, SMSMonitor.class));}
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

and Here is a part of my Service class:

public class SMSMonitor extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

    Intent i = new Intent(context,SMSMonitor.class);
    i.setClass(context, SMSMonitor.class);
    context.startService(i);

And here is my manifest File:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tabligh.block"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name="SMSMonitor">  
            <intent-filter>  
                <action android:name="android.intent.action.BOOT_COMPLETED" /> 
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action> 
            </intent-filter>  

        </receiver>

        <activity
            android:name=".TablighBlockActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:enabled="true" android:name=".SMSMonitor"
                                    android:process=":smsmonitor"
                                    ></service>
    </application>

</manifest>

and here is the error log:

06-22 13:46:14.649: W/dalvikvm(1013): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
06-22 13:46:14.668: E/AndroidRuntime(1013): Uncaught handler: thread main exiting due to uncaught exception
06-22 13:46:15.038: E/AndroidRuntime(1013): java.lang.RuntimeException: Unable to instantiate service tabligh.block.SMSMonitor: java.lang.ClassCastException: tabligh.block.SMSMonitor
06-22 13:46:15.038: E/AndroidRuntime(1013):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2449)
06-22 13:46:15.038: E/AndroidRuntime(1013):     at android.app.ActivityThread.access$2800(ActivityThread.java:112)
06-22 13:46:15.038: E/AndroidRuntime(1013):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744)
06-22 13:46:15.038: E/AndroidRuntime(1013):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 13:46:15.038: E/AndroidRuntime(1013):     at android.os.Looper.loop(Looper.java:123)
06-22 13:46:15.038: E/AndroidRuntime(1013):     at android.app.ActivityThread.main(ActivityThread.java:3948)
06-22 13:46:15.038: E/AndroidRuntime(1013):     at java.lang.reflect.Method.invokeNative(Native Method)
06-22 13:46:15.038: E/AndroidRuntime(1013):     at java.lang.reflect.Method.invoke(Method.java:521)
06-22 13:46:15.038: E/AndroidRuntime(1013):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
06-22 13:46:15.038: E/AndroidRuntime(1013):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
06-22 13:46:15.038: E/AndroidRuntime(1013):     at dalvik.system.NativeStart.main(Native Method)
06-22 13:46:15.038: E/AndroidRuntime(1013): Caused by: java.lang.ClassCastException: tabligh.block.SMSMonitor
06-22 13:46:15.038: E/AndroidRuntime(1013):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2446)
06-22 13:46:15.038: E/AndroidRuntime(1013):     ... 10 more

Thanks.

Upvotes: 3

Views: 6658

Answers (3)

Samir Mangroliya
Samir Mangroliya

Reputation: 40406

tabligh.block.SMSMonitor is receiver and service in your manifest file ?

i think it should be Service.and extends BroadCastReceiver to SMSMonitor.

 <receiver android:name="SMSMonitor"> **and**  

   <service android:enabled="true" android:name=".SMSMonitor"
                                    android:process=":smsmonitor"
                                    ></service>

AND

Intent i = new Intent(context,SMSMonitor.class);
//i.setClass(context, SMSMonitor.class);<<Remove this line no need...
context.startService(i);

Upvotes: 1

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

   public class SMSMonitor extends BroadcastReceiver {

you are SMSMonitor which is a BroadcastReceiver not Service

Intent i = new Intent(context,SMSMonitor.class);
    i.setClass(context, SMSMonitor.class);
    context.startService(i);

Upvotes: 3

telkins
telkins

Reputation: 10540

I've sometimes come across weird errors by not starting onCreate() with the standard super.onCreate() and setContentView().

And like others have said, make sure that you have added the Service to your Manifest file. This and forgetting to add Activities after I create them are my biggest mistake!

edit: Sorry, somehow missed all the Manifest code you put there.

Upvotes: 0

Related Questions