我在尝试启动服务时遇到“未找到活动”异常。我在Manifest中注册了服务。添加我认为的相关代码和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)
我的服务类:
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>
我的问题是: 当我在Manifest中注册了服务时,为什么会收到注册服务的错误消息?如何解决?
如果您需要更多信息,请与我们联系。欢呼声。
答案 0 :(得分:6)
您的方法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()
再次启动服务
答案 1 :(得分:2)
GPSService
是服务。它不是活动。你不能这样做
getActivity().startActivity(intent);
而非do startService(intent);
有关更多信息,请参阅Docs