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