在AsyncTask之后无法获得警报

时间:2012-04-07 14:57:40

标签: android facebook android-asynctask

我正在使用facebook跑步者来申请一些facebook api tesk。 我想检查我是否有一个带有数据或错误消息的JSON对象,所以我正在检查“消息”字段以检查它是否为空。 如果它不是null并且我有按摩,我想显示消息并退出应用程序。

这是我活动中的代码:

public class Loader extends Activity implements FacebookConnectionListener, ServerDataListener {
    private TextView loaderStatus;
    private Facebook facebook;
    private AsyncFacebookRunner facebookRunner;
    private FacebookConnection facebookConnection;   

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loader);
        facebook = new Facebook(facebookAppId);
        facebookRunner = new AsyncFacebookRunner(facebook);
        facebookConnection = new FacebookConnection(this, 1);
        facebookRunner.request("me",facebookConnection);
    }

    @Override
    public void onFacebookResponse(final String response , int step) {
        final JSONObject facebookResults = convertToJSON(response);
        final Context thisContext = (Context) getBaseContext();
        String id = "", name = "", gender = "", homeTown = "";
        Drawable profilePicture = null;

        if (step == 1) {
            if (facebookResults.isNull("message")) {
                try {
                    id = facebookResults.getString("id");
                    name = facebookResults.getString("name");
                    gender = facebookResults.getString("gender");
                    JSONObject homeTownObject = (JSONObject) facebookResults.get("hometown");
                    homeTown = homeTownObject.getString("name");
                    profilePicture = getProfilePicture(id);

                } catch (JSONException e) { 
                    alert("Facebook", e.toString(), "Ok", thisContext);
                }
                facebookCurrentUser = new FacebookCurrentUser(id, name, gender);
                facebookCurrentUser.setHomeTown(homeTown);
                facebookCurrentUser.setProfilePicture(profilePicture);
                app.setFacebookCurrentUser(facebookCurrentUser);
                facebookConnection = new FacebookConnection(this, 2);   
                this.runOnUiThread(new Runnable() {
                public void run() {
                        loaderStatus.setText("Getting friends details");
                    }
                });
            }
            else {
                this.runOnUiThread(new Runnable() {
                    public void run() {
                        alert("Facebook", "Can't get user details! Please try again", "Ok", thisContext);
                    }
                }); 
            }           
        }

public void alert (String title, String message, String ok, Context listener)
{
    AlertDialog.Builder alert = new AlertDialog.Builder(listener);
    alert.setMessage(message);
    alert.setTitle(title);
    alert.setPositiveButton(ok, new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();   
        }
    });
    alert.show();
}

这是FacebookConnection类:

public class FacebookConnection implements RequestListener {
    private FacebookConnectionListener listener;
    private int step;

    public FacebookConnection (FacebookConnectionListener listener, int step) {
        this.listener = listener;
        this.step = step;
    }

    public void updateFacebookListener(String response) {
        listener.onFacebookResponse(response, step);
    }

    @Override
    public void onComplete(String response, Object state) {
        updateFacebookListener(response);           
    }

    @Override
    public void onIOException(IOException e, Object state) { }

    @Override
    public void onFileNotFoundException(FileNotFoundException e, Object state) { }

    @Override
    public void onMalformedURLException(MalformedURLException e, Object state) { }

    @Override
    public void onFacebookError(FacebookError e, Object state) { }
 }

FacebookConnectionListener界面:

public interface FacebookConnectionListener {
    public void onFacebookResponse(String response, int step);
}

我的提醒一直给我:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

我无法理解为什么......帮助?

3 个答案:

答案 0 :(得分:0)

它出现的原因是因为你试图从另一个线程更新Ui。由于这是部分代码,我建议您将代码更改为

MyActivity.this.runOnUiThread(new Runnable() {
            public void run() {
                    loaderStatus.setText("Getting friends details");
                }
            });

其中MyActivity是您的活动名称。

答案 1 :(得分:0)

简单方法:

你可以发表出版物(某事)

并处理发布,以显示您的提醒:D

答案 2 :(得分:0)

好吧,试试这个:

public void alert (String title, String message, String ok) {
    this.runOnUiThread(new Runnable() {
        public void run() {
            AlertDialog alert = new AlertDialog.Builder(Loader.this).create();
            alert.setMessage(message);
            alert.setTitle(title);
            alert.setPositiveButton(ok, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            alert.show();
        }
    });
}

而不是你现在所拥有的,当你调用它时,只需像任何其他方法一样调用它:

this.alert("Facebook", "Can't get user details! Please try again", "Ok")

我不确定它出错的地方,甚至可能与你在构造对话框时忘记 create()部分有关。

希望这会为你排序。