问题描述
尝试启动服务时出现找不到活动"异常.我已在清单中注册了该服务.添加我认为是相关的代码和LogCat.让我知道是否需要查看更多内容.
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)
我的服务类别:
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.
推荐答案
您的方法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()
正在重新启动服务
you are using startActivity
instead of startService
. Also stopDrive()
is starting the service again
这篇关于尝试启动服务时出现ActivityNotFoundException.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!