• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Uncaught exception in service, really need help! [solved]

Hey Android devs!

I have quite the frustrating bug in my app. The setup is a graphical client and a Service polling for GPS coordinates and messages from the activity if it is bound.

After a random long time the app shuts down with an uncaught exception and I get the following messages in LogCat:
Code:
04-28 01:05:48.925: I/GLThread(8596): releasing EGL context because paused tid=10
04-28 01:05:48.995: I/GLThread(8596): noticed surfaceView surface lost tid=10
04-28 01:05:49.010: I/GLThread(8596): exiting tid=10
04-28 01:05:49.085: W/IInputConnectionWrapper(8596): showStatusIcon on inactive InputConnection
04-28 01:36:14.365: D/dalvikvm(8596): GC_EXPLICIT freed 1963K, 50% free 4471K/8903K, external 33K/544K, paused 132ms
04-28 01:37:50.455: D/dalvikvm(9091): GC_EXTERNAL_ALLOC freed 54K, 49% free 2746K/5379K, external 0K/0K, paused 21ms
04-28 01:37:50.475: D/AndroidRuntime(9091): Shutting down VM
04-28 01:37:50.475: W/dalvikvm(9091): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
04-28 01:37:50.480: E/AndroidRuntime(9091): FATAL EXCEPTION: main
04-28 01:37:50.480: E/AndroidRuntime(9091): java.lang.RuntimeException: Unable to create service se.bitheroes.rg.GPS_Service: java.lang.NullPointerException
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at android.app.ActivityThread.handleCreateService(ActivityThread.java:1959)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at android.app.ActivityThread.access$2500(ActivityThread.java:117)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at android.os.Looper.loop(Looper.java:130)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at android.app.ActivityThread.main(ActivityThread.java:3691)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at java.lang.reflect.Method.invokeNative(Native Method)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at java.lang.reflect.Method.invoke(Method.java:507)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at dalvik.system.NativeStart.main(Native Method)
04-28 01:37:50.480: E/AndroidRuntime(9091): Caused by: java.lang.NullPointerException
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at se.bitheroes.rg.eventSystem.EventHandler.LoadTriggers(EventHandler.java:93)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at se.bitheroes.rg.eventSystem.EventHandler.<init>(EventHandler.java:45)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at se.bitheroes.rg.GPS_Service.onCreate(GPS_Service.java:523)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	at android.app.ActivityThread.handleCreateService(ActivityThread.java:1949)
04-28 01:37:50.480: E/AndroidRuntime(9091): 	... 10 more
04-28 01:37:50.490: I/Process(9091): Sending signal. PID: 9091 SIG: 9
The NullPointerException should not be connected with this problem as it happens when the service tries to restart (and LibGdx is not initiated i assume, derp derp. Just another dumb thing I'm doing).

This is the code in the service's onCreate. I trimmed down some part so it's more readable
Code:
@Override
	public void onCreate()
	{
		super.onCreate();
		Toast.makeText( getApplicationContext(), "GPS_Service created", Toast.LENGTH_SHORT).show();
		
		locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		locationListener = new MyLocationListener();
		locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
		
		ServiceGlobals.eventHandler = new EventHandler(getExternalFilesDir(null).getAbsolutePath() + "/ServiceTriggers", true);
		
		ServiceLocationHandler.Init();
		ServiceChunkUpdate.Init();
		
		messageHandler = new MessageHandler();
		
		GPSCordinates = new ArrayBlockingQueue<GPSCoordinate>(256);
		
		//Start the map generating thread
		thread = new Thread() {
			public void run(){
				try {
				
				InitWorld();
				
				initialized = true;
				
				if(Statics.usingFakeCordinates)
				{
					latestOkLocation = new GPSCoordinate();
					latestOkLocation.lat = 0;
					latestOkLocation.lon = 0;
					latestOkLocation.gameLat = 0;
					latestOkLocation.gameLon = 0;
				}
					
				if(messageHandler.CanMessageOut())
				{
					Message msg = Message.obtain(null,MessageHandler.OUT_MSG_SERVICE_INITIALIZED);
					messageHandler.SendMessage(msg);
				}
				
				while(!killThread){
					//System.out.format("Thread loop \n");
					if(ProcessGPSCordinates())
					{
						System.out.format("Processing coordinate \n");
						
						ServiceMapHandler.Update(latestOkLocation.getGameX() , latestOkLocation.getGameY());
						
						---------------------
						
						messageHandler.SendMessage(outMsg);
					}
					
					//Check messages from the activity
					for(ServceMessage msg = messageHandler.GetNextMessage(); msg != null;  msg = messageHandler.GetNextMessage())
					{
						System.out.format("Activity message \n");
						
						switch(msg.GetType())
						{

							-----------------
						}   	
					}
					
					for(Action action = ServiceGlobals.eventHandler.GetNextAction(); action != null;  action = ServiceGlobals.eventHandler.GetNextAction())
					{
						System.out.format("Event message \n");
						switch(action.GetType())
							{	
							---------------------------
							}
					}
					
					if (ServiceChunkUpdate.UpdatedTiles() > 0) {
						if (!ServiceChunkUpdate.IsFull()) {
							Message outMsg = GenerateChunkUpdateMessage();
							messageHandler.SendMessage(outMsg);
						}
						else {
							for (int i = 0; i < ServiceChunkUpdate.chunkIndex; i++) {
								ServiceMapChunk chunk = ServiceChunkUpdate.chunks[i];
								
								short data[] = null;
								if (chunk != null)
									data = chunk.SerializeToActivityArray();
			                    
								if (data != null)
									messageHandler.SendMessage(GenerateChunkDataMessage(data, chunk.chunkPos.x, chunk.chunkPos.y));
							}
						}
					}
					
					if (ServiceChunkUpdate.updatedPaths.size() > 0) {
						Message outMsg2 = GeneratePathUpdateMessage(ServiceChunkUpdate.updatedPaths);
						messageHandler.SendMessage(outMsg2);
					}
					
					ServiceChunkUpdate.Flush();
					
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

				threadKilled = true;
				
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
		
		thread.start();
	}

Could this problem be caused by the Android system force shutting down the service? I have never been on the limit using up the phone's memory, but it seems as though it's often happening when i start up another application.

Help me out here. I'm a skilled programmer, but I have limited Java and Android knowledge. How can I better debug this? Where could the problem occur?

Let me know if you need to see more of the code to tell what could be the problem.
 
I've added code that makes the app go into foreground state. This solved the problem.

Looking at these lines, the PID is different, so it seems like the service is killed and tries to start again.
Code:
01:36:14.365: D/dalvikvm(8596): GC_EXPLICIT freed 1963K, 50% free 4471K/8903K, external 33K/544K, paused 132ms
04-28 01:37:50.455: D/dalvikvm(9091): GC_EXTERNAL_ALLOC freed 54K, 49% free 2746K/5379K, external 0K/0K, paused 21ms
04-28 01:37:50.475: D/AndroidRuntime(9091): Shutting down VM
04-28 01:37:50.475: W/dalvikvm(9091): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
The cause of this was because I used libGDX' "Files" interface in the service as well. When the service restarts it loses the references to the files interface which causes the exception.

I've rewritten loading of files to skip libGDX interface in the service and now it works like a charm.
I guess the tip I'd pass on is to look at the PID in LogCat, it could give you critical information.
 
Back
Top Bottom