Reputation: 1056
I'm working on my Android app and I'm having a bit of a weird problem. I'm trying to run a service from the main activity and with no luck. No matter what I tries no log messages appears. I found out that when erase the service statement out of the manifest I get the "not found" message, but when return it - nada, the log is empty tough I try to print.
I must say that I've tried to look for the answer for weeks and read a bunch of solution to some similar problems but again with no luck on that matter.
In the activity I do this:
startService (new Intent (this, GpsCompassService.class));
and in the service I do:
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);;
}
the 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>
What am I doing wrong?
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;
}
}
Here is the logcat file:
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)
Upvotes: 0
Views: 3939
Reputation: 48871
Assuming ManagerService
extends Service
then neither of the following methods are valid for a Service
and will never be called...
public void onCreate(Bundle savedInstanceState) {
Log.i("GpsCompassService onCreate", null);
}
//------------------------------------------------------------
public void onResume(){
Log.i("GpsCompassService has launched", null);;
The onCreate()
method of Service
doesn't take any parameters and onResume()
simply isn't a Service
method.
Delete onResume()
and change your onCreate()
code to something like...
@Override
public void onCreate() {
Log.i("GpsCompassService", "onCreate()");
}
EDIT: Also, don't use null
for the second (message) parameter when using Log
.
Upvotes: 1
Reputation: 6382
Like MisterSquonk said, remove your onResume()
method and change your onCreate()
method.
Then change the line
public class GpsCompassService extends ManagerService{
to
public class GpsCompassService extends Service{
The content of your ManagerService
declaration can be easily implemented into the Service's onCreate()
method or into a method like
@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
}
Also change the lines
Log.i("GpsCompassService onCreate", null);
to
Log.i("GpsCompassService onCreate", "some message");
Otherwise the app will crash, as the second parameter must not be null.
Upvotes: 1