DrkStr
DrkStr

Reputation: 1932

ActivityNotFoundException while trying to start a service.

I am getting an Activity not found exception while trying to start a service. I have the service registered in the Manifest. Adding what I think is the relevant code and the LogCat. Let me know if you need to see any more.

01-29 17:41:51.440    9196-9196/drvr.drvlog E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: drvr.drvlog, PID: 9196
android.content.ActivityNotFoundException: Unable to find explicit activity class {drvr.drvlog/drvr.drvlog.GPSService}; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1719)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1491)
        at android.app.Activity.startActivityForResult(Activity.java:3436)
        at android.app.Activity.startActivityForResult(Activity.java:3393)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
        at android.app.Activity.startActivity(Activity.java:3644)
        at android.app.Activity.startActivity(Activity.java:3607)
        at drvr.drvlog.HomeScreen.startDrive(HomeScreen.java:60)
        at drvr.drvlog.HomeScreen.onClick(HomeScreen.java:46)
        at android.view.View.performClick(View.java:4456)
        at android.view.View$PerformClick.run(View.java:18465)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

The my service class:

public class GPSService extends Service implements LocationListener {


SharedPreferences sp;
Utils util;
LocationManager locationManager;
DataBaseHelper db;

long driveId;

//empty constructor.
public GPSService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    util = new Utils();

    sp = getSharedPreferences(util.APP_DATA, 0);

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    db = new DataBaseHelper(this);

    boolean driving = sp.getBoolean(util.DRIVING, false);

    if (intent != null) {
        if (intent.getAction() == util.STOP_DRIVE) {
            if (driving)
                stopDrive();
        } else if (intent.getAction() == util.START_DRIVE) {
            if (!driving)
                startDrive();
        }
    }

    return START_REDELIVER_INTENT;
}

I am calling the starting the service from inside a fragment. here is where I am starting the service.

private void startDrive()
{
    Log.w("rakshak", "Start drive clicked");

    Intent intent = new Intent(getActivity(), GPSService.class);
    intent.setAction(util.START_DRIVE);
    getActivity().startActivity(intent);
}

private void stopDrive()
{
    Log.w("rakshak","Stop Drive clicked");

    Intent intent = new Intent(getActivity(), GPSService.class);
    intent.setAction(util.STOP_DRIVE);
    getActivity().startService(intent);
}

And in my manifest I have this in my application tag

<service android:name=".GPSService"></service>

My question is: Why am I getting the error message for registering the service when I have the service registered in the Manifest and how do I fix it ?

Let me know if you need any more info. Cheers.

Upvotes: 2

Views: 449

Answers (2)

Blackbelt
Blackbelt

Reputation: 157447

your method startDrive is wrong

private void startDrive()
{
    Log.w("rakshak", "Start drive clicked");

    Intent intent = new Intent(getActivity(), GPSService.class);
    intent.setAction(util.START_DRIVE);
    getActivity().startActivity(intent);
}

you are using startActivity instead of startService. Also stopDrive() is starting the service again

Upvotes: 6

M D
M D

Reputation: 47817

GPSService is a service. It is not an Activity. You cant do as

 getActivity().startActivity(intent);

instead of do startService(intent);

For more information refer Docs

Upvotes: 2

Related Questions