1

I'm using an async task to download a json string from my server. This is the code I'm using:

How I use it is, first I open the profile activity, then on the oncreate event on the profile activity, I call this async task to get the information. When this finishes it calls a function on the profile activity using its context to display the information. If some error occurred, then I call the finish function on the activity and display a message.

The problem is, the first time when I visit the profile activity, it works fine, then the second time it goes into a black screen. The interesting thing is I can press back and it will end the black screen but go back to previous activity (the one where I click the button to open the profile activity).

Does anyone know whats the problem here?

Thanks.

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import sord.http.Http;
import sord.ids_connect.Activity_Profile;
import sord.ids_connect.R;
import sord.object.User;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.util.Pair;

/*
 * This asynchronous task gets profile data.
 * 
 */
public class Async_get_profile extends AsyncTask<String, Void, Pair<String,Integer>> {

    Activity_Profile callerContext = null;
    ProgressDialog progressDialog = null;
    int targetID;

    public Async_get_profile(Activity_Profile callerContext, int targetID) {
        this.callerContext = callerContext;
        this.targetID = targetID;
    }

    @Override
    protected void onPreExecute() {  
        super.onPreExecute();
        progressDialog = new ProgressDialog((Context) callerContext);
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(false);
        progressDialog.show();
    } 

    @Override
    protected Pair<String,Integer> doInBackground(String... params) {
        List<NameValuePair> urlValues = new ArrayList<NameValuePair>();

        urlValues.add(new BasicNameValuePair("taskID", "5"));
        urlValues.add(new BasicNameValuePair("userID", Integer.toString(User.currentUser.id, 10)));
        urlValues.add(new BasicNameValuePair("targetID", Integer.toString(targetID, 10)));

        Pair<String,Integer> response = Http.GetHTTPResponse(((Context) callerContext).getResources().getString(R.string.host), urlValues);

        return response;
    }

    @Override
    protected void onPostExecute(Pair<String,Integer> responsePair) {
        try {
            JSONObject object = Http.ValidateObjectResponse(((Context) callerContext), responsePair);

            if (object != null) {

                String firstname = object.getString("firstname");
                String lastname = object.getString("lastname");
                String password = object.getString("password");
                String email = object.getString("email");
                String phone = object.getString("phone");
                String website = object.getString("website");
                String status = object.getString("status");
                String biotext = object.getString("biotext");
                int roldId = object.getInt("role_id");
                String datejoined = object.getString("datejoined");
                String datelastactive = object.getString("datelastactive");
                int active = object.getInt("active");
                String picURL = object.getString("picURL");

                if (targetID == User.currentUser.id) {
                    User.currentUser.firstName = firstname;
                    User.currentUser.lastName = lastname;
                    User.currentUser.password = password;
                    User.currentUser.emailAddress = email;
                    User.currentUser.phoneNumber = phone;
                    User.currentUser.websiteLink = website;
                    User.currentUser.statusText = status;
                    User.currentUser.bioText = biotext;
                    User.currentUser.dateJoined = datejoined;
                    User.currentUser.dateLastActive = datelastactive;
                    User.currentUser.picURL = picURL;

                    callerContext.ProfileCallback(User.currentUser);
                } else {
                    User user = new User(
                            false, 
                            targetID, 
                            firstname,
                            lastname,
                            password,
                            email,
                            phone,
                            website,
                            status,
                            biotext,
                            roldId,
                            datejoined,
                            datelastactive,
                            active,
                            picURL
                    );

                    callerContext.ProfileCallback(user);
                }
            } else {
                callerContext.finish();
            }
        } catch (Exception e) {
            Log.d("ERROR", e.getMessage());
            callerContext.finish();
        }

        progressDialog.dismiss();
    }
}

EDIT:

Logcat

log.txt

omega
  • 40,311
  • 81
  • 251
  • 474
  • Okay, lets get some more info..what is your logcat output showing? – Fred Grott Dec 30 '13 at 23:46
  • ok I put a link to the logcat output. – omega Dec 31 '13 at 00:05
  • I'm not sure the problem has anything to do with this code, as the logcat is full of errors about trying to draw a recycled bitmap. – Greg Ennis Dec 31 '13 at 00:10
  • oh, i did try to recycle bitmaps, maybe thats the problem. I do that when an activity ends. Maybe i should not recycle them. – omega Dec 31 '13 at 00:14
  • It depends - If they are static variables, you should not recycle them. However if you are re-creating them in onCreate then you should recycle. – Greg Ennis Dec 31 '13 at 00:16
  • hmm, now that I comment out the code which does recycling, it works.... – omega Dec 31 '13 at 00:22
  • ok but make sure you don't have a resource leak, because you do need to recycle images when you are done with them. – Greg Ennis Dec 31 '13 at 00:27
  • What if I download an image from the web and set it to a imageview on the oncreate event, then do I need to recycle it when the activity ends? Because I tried to recycle them on the ondestroy event, but then had those issues with black screen. – omega Dec 31 '13 at 00:34
  • That tells me you are storing the image somewhere and re-using it, in which case you should not recycle it. OS will clean it up when it kills your app – Greg Ennis Dec 31 '13 at 00:44
  • I found a good discussion of it here on so: http://stackoverflow.com/questions/7009086/recycle-imageviews-bitmap – Greg Ennis Dec 31 '13 at 00:47
  • That is probably it the image recycling.. – Fred Grott Dec 31 '13 at 01:54
  • Check the sdk samples, I seem to remember specific example of how to recycle bitmaps in a certain way – Fred Grott Dec 31 '13 at 01:57

0 Answers0