Question

I am running an AlarmManager that fires a Serivce every x Seconds by using this:

Intent intent = new Intent(AndroidGPSTrackingActivity.this,
        AppService.class);
PendingIntent sender =
        PendingIntent.getService(AndroidGPSTrackingActivity.this,
                0, intent, 0);

AlarmManager am = (AlarmManager)
        getSystemService(ALARM_SERVICE);
am.cancel(sender);

The service class is declared with: <service android:name=".AppService" /> in the manifest.

AppService.java:

public class AppService extends Service {
    GPSTracker gps;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        //Toast.makeText(getApplicationContext(), "Service Created", 1).show();
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        //Toast.makeText(getApplicationContext(), "Service Destroy", 1).show();
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        //Toast.makeText(getApplicationContext(), "Service Running ", 1).show();
        PowerManager pm = (PowerManager) this.getSystemService(this.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();

        gps = new GPSTracker(this);

        if (gps.canGetLocation()) {

            double latitude = gps.getLatitude();
            double longitude = gps.getLongitude();
            SendLocation SendLocation = new SendLocation();
            SendLocation.execute(latitude, longitude);
        } else {
            gps.showSettingsAlert();
        }
        wl.release();
        return super.onStartCommand(intent, flags, startId);
    }


}

SendLocation.java:

public class SendLocation extends AsyncTask<Object, Void, Void> {//line 18
    private Double latitude, longitude;
    private String userName, password;
    GlobalClass Global = new GlobalClass();

    protected Void doInBackground(Object... params) {
        latitude = (Double) params[0];
        longitude = (Double) params[1];

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Global.getHostServer());//line 28

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("strLat", Double
                    .toString(latitude)));
            nameValuePairs.add(new BasicNameValuePair("strLong", Double
                    .toString(longitude)));
            nameValuePairs.add(new BasicNameValuePair("userName", Global.getUserName()));
            nameValuePairs.add(new BasicNameValuePair("password", Global.getPassword()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            String responseStr = EntityUtils.toString(response.getEntity());

            Log.d("Response",responseStr);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
        return null;
    }
}

It works for awhile, then suddenly I get two errors, one on line 18 and one on 28, then eventually... A force close. Any ideas why I'm getting this force close?

UPDATE: Turns out to be a definite forceclose when android kills the app automatically when trying to free up memory. How can I fix this issue?

LogCat:

    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
            at java.net.URI.parseURI(URI.java:353)
            at java.net.URI.<init>(URI.java:204)
            at java.net.URI.create(URI.java:725)
            at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
            at com.example.gpstracking.SendLocation.doInBackground(SendLocation.java:28)
            at com.example.gpstracking.SendLocation.doInBackground(SendLocation.java:18)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
03-10 00:43:32.803    1693-1693/com.example.gpstracking E/Trace﹕ error opening trace file: No such file or directory (2)
03-10 00:43:32.979    1693-1708/com.example.gpstracking E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
            at java.net.URI.parseURI(URI.java:353)
            at java.net.URI.<init>(URI.java:204)
            at java.net.URI.create(URI.java:725)
            at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
            at com.example.gpstracking.SendLocation.doInBackground(SendLocation.java:28)
            at com.example.gpstracking.SendLocation.doInBackground(SendLocation.java:18)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
Was it helpful?

Solution 2

It turns out that on App Force Close, it would close out my Global Class and I'd be without a Global variables. I fixed it by having every bit of information passed as params like so:

protected Void doInBackground(Object... params) {
    latitude = (Double) params[0];
    longitude = (Double) params[1];
    userName = (String) params[2];
    password = (String) params[3];
    hostServer = (String) params[4];

And have it called in service like:

SendLocation.execute(latitude, longitude, Global.getUserName(), Global.getPassword(), Global.getHostServer());

OTHER TIPS

The only weird thing I see in your code related to the Params parameter of you AsyncTask. Your declaration looks like this:

public class SendLocation extends AsyncTask<Object, Void, Void> 

Where the Params is defined as Object, but when invoking execute your're passing latitude and longitude which are variables of type double.

SendLocation.execute(latitude, longitude);

You should change your definition of your AsyncTask like this:

public class SendLocation extends AsyncTask<Double, Void, Void> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top