我在我的Android应用程序上工作,我有一个奇怪的问题。 我试图从主要活动运行服务,没有运气。无论我尝试什么,都不会出现日志消息。我发现当从清单中删除服务语句时,我得到“未找到”消息,但是当它返回时 - nada,日志是空的,我试图打印。
我必须说,我试图寻找几个星期的答案并阅读一些类似问题的解决方案但又没有运气。
在活动中我这样做:
startService(new Intent(this,GpsCompassService.class));
在我所做的服务中:
`
public class GpsCompassService extends ManagerService{
private static final String TAG = "GpsCompassServiceActivity";
public void onCreate(Bundle savedInstanceState) {
Log.i("GpsCompassService onCreate", null);
}
//------------------------------------------------------------
public void onResume(){
Log.i("GpsCompassService has launched", null);;
}`
manifst.xml: `
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="project.main.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application android:name=".ManagerApplication" android:icon="@drawable/icon" android:label="@string/app_name" >
<activity android:label="@string/app_name" android:name=".MenuActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="@string/app_name" android:name="project.main.app.CameraPreviewActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service android:enabled="true" android:name="project.main.app.GpsCompassService">
<intent-filter>
<action android:name="project.main.app.GpsCompassService"></action>
</intent-filter>
</service>
</application>
</manifest>
`
我做错了什么?
提前10倍!package project.main.app;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class ManagerService extends Service {
public ManagerApplication getManagerApp(){
ManagerApplication appSharedDate = (ManagerApplication)getApplication();
return appSharedDate;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这是logcat文件:
01-07 20:19:29.281: E/AndroidRuntime(557): FATAL EXCEPTION: main
01-07 20:19:29.281: E/AndroidRuntime(557): java.lang.RuntimeException: Unable to create service project.main.app.GpsCompassService: java.lang.NullPointerException: println needs a message
01-07 20:19:29.281: E/AndroidRuntime(557): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1955)
01-07 20:19:29.281: E/AndroidRuntime(557): at android.app.ActivityThread.access$2500(ActivityThread.java:117)
01-07 20:19:29.281: E/AndroidRuntime(557): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
01-07 20:19:29.281: E/AndroidRuntime(557): at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 20:19:29.281: E/AndroidRuntime(557): at android.os.Looper.loop(Looper.java:130)
01-07 20:19:29.281: E/AndroidRuntime(557): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-07 20:19:29.281: E/AndroidRuntime(557): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 20:19:29.281: E/AndroidRuntime(557): at java.lang.reflect.Method.invoke(Method.java:507)
01-07 20:19:29.281: E/AndroidRuntime(557): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-07 20:19:29.281: E/AndroidRuntime(557): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-07 20:19:29.281: E/AndroidRuntime(557): at dalvik.system.NativeStart.main(Native Method)
01-07 20:19:29.281: E/AndroidRuntime(557): Caused by: java.lang.NullPointerException: println needs a message
01-07 20:19:29.281: E/AndroidRuntime(557): at android.util.Log.println_native(Native Method)
01-07 20:19:29.281: E/AndroidRuntime(557): at android.util.Log.i(Log.java:158)
01-07 20:19:29.281: E/AndroidRuntime(557): at project.main.app.GpsCompassService.onCreate(GpsCompassService.java:32)
01-07 20:19:29.281: E/AndroidRuntime(557): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1945)
答案 0 :(得分:1)
假设ManagerService
扩展Service
,则以下任何一种方法都不适用于Service
,且永远不会被调用...
public void onCreate(Bundle savedInstanceState) {
Log.i("GpsCompassService onCreate", null);
}
//------------------------------------------------------------
public void onResume(){
Log.i("GpsCompassService has launched", null);;
onCreate()
Service
方法不接受任何参数,onResume()
根本不是Service
方法。
删除onResume()
并将您的onCreate()
代码更改为......
@Override
public void onCreate() {
Log.i("GpsCompassService", "onCreate()");
}
编辑:此外,使用null
时,请勿使用Log
作为第二个(消息)参数。
答案 1 :(得分:1)
像MisterSquonk所说,删除onResume()
方法并更改onCreate()
方法。
然后更改行
public class GpsCompassService extends ManagerService{
到
public class GpsCompassService extends Service{
您的ManagerService
声明的内容可以轻松实现到服务的onCreate()
方法或类似
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStart(intent, startId);
doSomething();
return START_NOT_STICKY; // or START_STICKY, depending on your needs
}
同时更改行
Log.i("GpsCompassService onCreate", null);
到
Log.i("GpsCompassService onCreate", "some message");
否则应用程序将崩溃,因为第二个参数不能为空。