AsyncTask和回调Android

时间:2012-04-20 05:02:12

标签: java android facebook-android-sdk

我需要使用facebok登录,因此我下载了sdk并创建了我的方法。

我收到用户登录信息后,我需要将其发送到我的网络服务并回复我的活动。

这是我的代码。 onComplete我试图解释我需要什么: 基本上我需要回调我当前的活动。

public class DisplayLoginActivity extends FragmentActivity {



    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_login_activity);

        login();
    }

    public void login(){
        facebook = new Facebook(kAppId);

        facebook.authorize(this, new DialogListener() {

            @Override
            public void onComplete(Bundle values) {
                System.out.println("onComplete");
                        JSONObject me = null;
                        me = new JSONObject(facebook.request("me"));                

                        String id = me.getString("id");     

                        //I want here to call other async request and make it call back to DisplayLoginActivity, for that
                        //reason i created callback with interface so i need to give this of my Activity
                        //If i give this so it's DialogListener this. I tried to do this DisplayLoginActivity.this but it crashes
            }


            @Override
            public void onFacebookError(FacebookError error) {
                error.getMessage();

            }

            @Override
            public void onError(DialogError e) {

            }

            @Override
            public void onCancel() {

            }

        });
    }

}

也许你有一些想法? 感谢。

修改

04-20 08:34:50.270: E/AndroidRuntime(7642): FATAL EXCEPTION: Thread-10
04-20 08:34:50.270: E/AndroidRuntime(7642): java.lang.ExceptionInInitializerError
04-20 08:34:50.270: E/AndroidRuntime(7642):     at com.my.main.DisplayLoginActivity$1$1.run(DisplayLoginActivity.java:161)
04-20 08:34:50.270: E/AndroidRuntime(7642):     at java.lang.Thread.run(Thread.java:1019)
04-20 08:34:50.270: E/AndroidRuntime(7642): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-20 08:34:50.270: E/AndroidRuntime(7642):     at android.os.Handler.<init>(Handler.java:121)
04-20 08:34:50.270: E/AndroidRuntime(7642):     at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
04-20 08:34:50.270: E/AndroidRuntime(7642):     at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
04-20 08:34:50.270: E/AndroidRuntime(7642):     at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
04-20 08:34:50.270: E/AndroidRuntime(7642):     ... 2 more

1 个答案:

答案 0 :(得分:1)

我不熟悉Facebook API,但您是否尝试在单独的线程(在本例中为onComplete())上运行的函数中调用Looper.prepare()?它应该是线程中的第一个函数调用。