尝试启动服务时出现 ActivityNotFoundException。

ActivityNotFoundException while trying to start a service.

我在尝试启动服务时收到 Activity 未找到异常。我在清单中注册了服务。添加我认为是相关代码和LogCat。让我知道您是否需要查看更多内容。

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)

我的服务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;
}

我正在调用从片段内部启动服务。这是我开始服务的地方。

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

在我的清单中,我的应用程序标签中有这个

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

我的问题是: 为什么我在清单中注册了服务后却收到注册服务的错误消息,我该如何解决?

如果您需要更多信息,请告诉我。干杯。

GPSService 是一个 服务 。它不是 Activity。你不能像

 getActivity().startActivity(intent);

而不是 startService(intent);

有关详细信息,请参阅 Docs

你的方法startDrive是错误的

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

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

您正在使用 startActivity 而不是 startService。另外 stopDrive() 正在再次启动服务