1. Download our Official Android App: Forums for Android!

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

Discussion in 'Android Development' started by Gauss Jordan, Apr 28, 2012.

  1. Gauss Jordan

    Gauss Jordan Lurker
    Thread Starter
    Rank:
    None
    Points:
    26
    Posts:
    7
    Joined:
    Dec 21, 2011

    Dec 21, 2011
    7
    1
    26
    Developer
    Sweden
    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 (Text):
    1. 04-28 01:05:48.925: I/GLThread(8596): releasing EGL context because paused tid=10
    2. 04-28 01:05:48.995: I/GLThread(8596): noticed surfaceView surface lost tid=10
    3. 04-28 01:05:49.010: I/GLThread(8596): exiting tid=10
    4. 04-28 01:05:49.085: W/IInputConnectionWrapper(8596): showStatusIcon on inactive InputConnection
    5. 04-28 01:36:14.365: D/dalvikvm(8596): GC_EXPLICIT freed 1963K, 50% free 4471K/8903K, external 33K/544K, paused 132ms
    6. 04-28 01:37:50.455: D/dalvikvm(9091): GC_EXTERNAL_ALLOC freed 54K, 49% free 2746K/5379K, external 0K/0K, paused 21ms
    7. 04-28 01:37:50.475: D/AndroidRuntime(9091): Shutting down VM
    8. 04-28 01:37:50.475: W/dalvikvm(9091): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
    9. 04-28 01:37:50.480: E/AndroidRuntime(9091): FATAL EXCEPTION: main
    10. 04-28 01:37:50.480: E/AndroidRuntime(9091): java.lang.RuntimeException: Unable to create service se.bitheroes.rg.GPS_Service: java.lang.NullPointerException
    11. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:1959)
    12. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at android.app.ActivityThread.access$2500(ActivityThread.java:117)
    13. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989)
    14. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at android.os.Handler.dispatchMessage(Handler.java:99)
    15. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at android.os.Looper.loop(Looper.java:130)
    16. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at android.app.ActivityThread.main(ActivityThread.java:3691)
    17. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at java.lang.reflect.Method.invokeNative(Native Method)
    18. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at java.lang.reflect.Method.invoke(Method.java:507)
    19. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
    20. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
    21. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at dalvik.system.NativeStart.main(Native Method)
    22. 04-28 01:37:50.480: E/AndroidRuntime(9091): Caused by: java.lang.NullPointerException
    23. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at se.bitheroes.rg.eventSystem.EventHandler.LoadTriggers(EventHandler.java:93)
    24. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at se.bitheroes.rg.eventSystem.EventHandler.<init>(EventHandler.java:45)
    25. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at se.bitheroes.rg.GPS_Service.onCreate(GPS_Service.java:523)
    26. 04-28 01:37:50.480: E/AndroidRuntime(9091):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:1949)
    27. 04-28 01:37:50.480: E/AndroidRuntime(9091):     ... 10 more
    28. 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 (Text):
    1. @Override
    2.     public void onCreate()
    3.     {
    4.         super.onCreate();
    5.         Toast.makeText( getApplicationContext(), "GPS_Service created", Toast.LENGTH_SHORT).show();
    6.        
    7.         locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    8.         locationListener = new MyLocationListener();
    9.         locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
    10.        
    11.         ServiceGlobals.eventHandler = new EventHandler(getExternalFilesDir(null).getAbsolutePath() + "/ServiceTriggers", true);
    12.        
    13.         ServiceLocationHandler.Init();
    14.         ServiceChunkUpdate.Init();
    15.        
    16.         messageHandler = new MessageHandler();
    17.        
    18.         GPSCordinates = new ArrayBlockingQueue<GPSCoordinate>(256);
    19.        
    20.         //Start the map generating thread
    21.         thread = new Thread() {
    22.             public void run(){
    23.                 try {
    24.                
    25.                 InitWorld();
    26.                
    27.                 initialized = true;
    28.                
    29.                 if(Statics.usingFakeCordinates)
    30.                 {
    31.                     latestOkLocation = new GPSCoordinate();
    32.                     latestOkLocation.lat = 0;
    33.                     latestOkLocation.lon = 0;
    34.                     latestOkLocation.gameLat = 0;
    35.                     latestOkLocation.gameLon = 0;
    36.                 }
    37.                    
    38.                 if(messageHandler.CanMessageOut())
    39.                 {
    40.                     Message msg = Message.obtain(null,MessageHandler.OUT_MSG_SERVICE_INITIALIZED);
    41.                     messageHandler.SendMessage(msg);
    42.                 }
    43.                
    44.                 while(!killThread){
    45.                     //System.out.format("Thread loop \n");
    46.                     if(ProcessGPSCordinates())
    47.                     {
    48.                         System.out.format("Processing coordinate \n");
    49.                        
    50.                         ServiceMapHandler.Update(latestOkLocation.getGameX() , latestOkLocation.getGameY());
    51.                        
    52.                         ---------------------
    53.                        
    54.                         messageHandler.SendMessage(outMsg);
    55.                     }
    56.                    
    57.                     //Check messages from the activity
    58.                     for(ServceMessage msg = messageHandler.GetNextMessage(); msg != null;  msg = messageHandler.GetNextMessage())
    59.                     {
    60.                         System.out.format("Activity message \n");
    61.                        
    62.                         switch(msg.GetType())
    63.                         {
    64.  
    65.                             -----------------
    66.                         }      
    67.                     }
    68.                    
    69.                     for(Action action = ServiceGlobals.eventHandler.GetNextAction(); action != null;  action = ServiceGlobals.eventHandler.GetNextAction())
    70.                     {
    71.                         System.out.format("Event message \n");
    72.                         switch(action.GetType())
    73.                             {  
    74.                             ---------------------------
    75.                             }
    76.                     }
    77.                    
    78.                     if (ServiceChunkUpdate.UpdatedTiles() > 0) {
    79.                         if (!ServiceChunkUpdate.IsFull()) {
    80.                             Message outMsg = GenerateChunkUpdateMessage();
    81.                             messageHandler.SendMessage(outMsg);
    82.                         }
    83.                         else {
    84.                             for (int i = 0; i < ServiceChunkUpdate.chunkIndex; i++) {
    85.                                 ServiceMapChunk chunk = ServiceChunkUpdate.chunks[i];
    86.                                
    87.                                 short data[] = null;
    88.                                 if (chunk != null)
    89.                                     data = chunk.SerializeToActivityArray();
    90.                                
    91.                                 if (data != null)
    92.                                     messageHandler.SendMessage(GenerateChunkDataMessage(data, chunk.chunkPos.x, chunk.chunkPos.y));
    93.                             }
    94.                         }
    95.                     }
    96.                    
    97.                     if (ServiceChunkUpdate.updatedPaths.size() > 0) {
    98.                         Message outMsg2 = GeneratePathUpdateMessage(ServiceChunkUpdate.updatedPaths);
    99.                         messageHandler.SendMessage(outMsg2);
    100.                     }
    101.                    
    102.                     ServiceChunkUpdate.Flush();
    103.                    
    104.                     try {
    105.                         Thread.sleep(10);
    106.                     } catch (InterruptedException e) {
    107.                         e.printStackTrace();
    108.                     }
    109.                 }
    110.  
    111.                 threadKilled = true;
    112.                
    113.                 } catch (Exception e) {
    114.                     e.printStackTrace();
    115.                 }
    116.             }
    117.         };
    118.        
    119.         thread.start();
    120.     }
    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.
     

    Advertisement

  2. Gauss Jordan

    Gauss Jordan Lurker
    Thread Starter
    Rank:
    None
    Points:
    26
    Posts:
    7
    Joined:
    Dec 21, 2011

    Dec 21, 2011
    7
    1
    26
    Developer
    Sweden
    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 (Text):
    1. 01:36:14.365: D/dalvikvm(8596): GC_EXPLICIT freed 1963K, 50% free 4471K/8903K, external 33K/544K, paused 132ms
    2. 04-28 01:37:50.455: D/dalvikvm(9091): GC_EXTERNAL_ALLOC freed 54K, 49% free 2746K/5379K, external 0K/0K, paused 21ms
    3. 04-28 01:37:50.475: D/AndroidRuntime(9091): Shutting down VM
    4. 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.
     

Share This Page

Loading...