导致黑屏的Android异步任务

时间:2013-12-30 23:37:51

标签: android android-asynctask crash

我正在使用异步任务从我的服务器下载json字符串。这是我正在使用的代码:

我如何使用它是,首先我打开配置文件活动,然后在配置文件活动的oncreate事件上,我调用此异步任务来获取信息。完成后,它会使用其上下文调用配置文件活动上的函数来显示信息。如果发生了一些错误,那么我会在活动上调用finish函数并显示一条消息。

问题是,我第一次访问个人资料活动时,它工作正常,然后第二次进入黑屏。有趣的是我可以按下它,它将结束黑屏但返回上一个活动(我点击按钮打开配置文件活动的那个)。

有谁知道这里的问题是什么?

感谢。

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();
    }
}

修改

logcat的

log.txt

0 个答案:

没有答案