如何在我的Android应用程序中集成Facebook?

时间:2013-03-12 09:44:43

标签: android facebook

我正在为facebook整合编写代码。我创建了App Id并导入了facebook sdk.I在我的主要活动上放了一个按钮,这将打开一个facebook对话框,询问登录ID和密码。但点击显示“强制关闭”的AVD。在那个按钮里面我调用了以下功能:

public void loginToFacebook() {
        mPrefs = getPreferences(MODE_PRIVATE);
        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);

        if (access_token != null) {
            facebook.setAccessToken(access_token);
        }

        if (expires != 0) {
            facebook.setAccessExpires(expires);
        }

        if (!facebook.isSessionValid()) {
            facebook.authorize(this,
                    new String[] { "email", "publish_stream" },
                    new DialogListener() {

                        @Override
                        public void onCancel() {
                            // Function to handle cancel event
                        }

                        @Override
                        public void onComplete(Bundle values) {
                            // Function to handle complete event
                            // Edit Preferences and update facebook acess_token
                            SharedPreferences.Editor editor = mPrefs.edit();
                            editor.putString("access_token",
                                    facebook.getAccessToken());
                            editor.putLong("access_expires",
                                    facebook.getAccessExpires());
                            editor.commit();
                        }



                        @Override
                        public void onFacebookError(FacebookError e) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onError(DialogError e) {
                            // TODO Auto-generated method stub

                        }

                    });
        }
        }

以下是logcat:

03-12 14:57:44.676: W/com.facebook.Session(499): Should not pass a read permission (email) to a request for publish or manage authorization
03-12 14:57:44.861: W/dalvikvm(499): VFY: unable to resolve static field 1431 (com_facebook_login_activity_layout) in Lcom/facebook/android/R$layout;
03-12 14:57:44.872: D/dalvikvm(499): VFY: replacing opcode 0x60 at 0x0003
03-12 14:57:44.872: D/dalvikvm(499): VFY: dead code 0x0005-0051 in Lcom/facebook/LoginActivity;.onCreate (Landroid/os/Bundle;)V
03-12 14:57:44.876: W/dalvikvm(499): VFY: unable to resolve static field 1413 (com_facebook_login_activity_progress_bar) in Lcom/facebook/android/R$id;
03-12 14:57:44.876: D/dalvikvm(499): VFY: replacing opcode 0x60 at 0x0008
03-12 14:57:44.876: D/dalvikvm(499): VFY: dead code 0x000a-0013 in Lcom/facebook/LoginActivity;.onPause ()V
03-12 14:57:45.126: W/dalvikvm(499): VFY: unable to resolve static field 1440 (com_facebook_internet_permission_error_title) in Lcom/facebook/android/R$string;
03-12 14:57:45.126: D/dalvikvm(499): VFY: replacing opcode 0x60 at 0x0010
03-12 14:57:45.126: D/dalvikvm(499): VFY: dead code 0x0012-0026 in Lcom/facebook/AuthorizationClient;.checkInternetPermission ()Z
03-12 14:57:45.986: W/dalvikvm(499): VFY: unable to resolve static field 1407 (com_facebook_close) in Lcom/facebook/android/R$drawable;
03-12 14:57:45.986: D/dalvikvm(499): VFY: replacing opcode 0x60 at 0x001d
03-12 14:57:46.006: D/dalvikvm(499): VFY: dead code 0x001f-002e in Lcom/facebook/widget/WebDialog;.createCrossImage ()V
03-12 14:57:46.026: W/dalvikvm(499): VFY: unable to resolve static field 1441 (com_facebook_loading) in Lcom/facebook/android/R$string;
03-12 14:57:46.026: D/dalvikvm(499): VFY: replacing opcode 0x60 at 0x0024
03-12 14:57:46.036: D/dalvikvm(499): VFY: dead code 0x0026-006d in Lcom/facebook/widget/WebDialog;.onCreate (Landroid/os/Bundle;)V
03-12 14:57:46.070: D/AndroidRuntime(499): Shutting down VM
03-12 14:57:46.070: W/dalvikvm(499): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-12 14:57:46.243: E/AndroidRuntime(499): FATAL EXCEPTION: main
03-12 14:57:46.243: E/AndroidRuntime(499): java.lang.NoClassDefFoundError: com.facebook.android.R$string
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.facebook.widget.WebDialog.onCreate(WebDialog.java:188)
03-12 14:57:46.243: E/AndroidRuntime(499):  at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
03-12 14:57:46.243: E/AndroidRuntime(499):  at android.app.Dialog.show(Dialog.java:225)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.facebook.AuthorizationClient$WebViewAuthHandler.tryAuthorize(AuthorizationClient.java:461)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.facebook.AuthorizationClient.tryCurrentHandler(AuthorizationClient.java:209)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.facebook.AuthorizationClient.tryNextHandler(AuthorizationClient.java:188)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.facebook.AuthorizationClient.authorize(AuthorizationClient.java:116)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.facebook.AuthorizationClient.startOrContinueAuth(AuthorizationClient.java:97)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.facebook.Session.tryLegacyAuth(Session.java:1096)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.facebook.Session.authorize(Session.java:893)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.facebook.Session.open(Session.java:957)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.facebook.Session.openForPublish(Session.java:415)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.facebook.android.Facebook.openSession(Facebook.java:319)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.facebook.android.Facebook.authorize(Facebook.java:313)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.facebook.android.Facebook.authorize(Facebook.java:149)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.example.kumarfbapp.FBActivity.loginToFacebook(FBActivity.java:61)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.example.kumarfbapp.FBActivity$1.onClick(FBActivity.java:40)
03-12 14:57:46.243: E/AndroidRuntime(499):  at android.view.View.performClick(View.java:2408)
03-12 14:57:46.243: E/AndroidRuntime(499):  at android.view.View$PerformClick.run(View.java:8816)
03-12 14:57:46.243: E/AndroidRuntime(499):  at android.os.Handler.handleCallback(Handler.java:587)
03-12 14:57:46.243: E/AndroidRuntime(499):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 14:57:46.243: E/AndroidRuntime(499):  at android.os.Looper.loop(Looper.java:123)
03-12 14:57:46.243: E/AndroidRuntime(499):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-12 14:57:46.243: E/AndroidRuntime(499):  at java.lang.reflect.Method.invokeNative(Native Method)
03-12 14:57:46.243: E/AndroidRuntime(499):  at java.lang.reflect.Method.invoke(Method.java:521)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-12 14:57:46.243: E/AndroidRuntime(499):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-12 14:57:46.243: E/AndroidRuntime(499):  at dalvik.system.NativeStart.main(Native Method)
03-12 14:57:52.052: I/Process(499): Sending signal. PID: 499 SIG: 9
03-12 15:02:48.486: W/com.facebook.Session(531): Should not pass a read permission (email) to a request for publish or manage authorization
03-12 15:02:48.506: W/dalvikvm(531): VFY: unable to resolve static field 1431 (com_facebook_login_activity_layout) in Lcom/facebook/android/R$layout;
03-12 15:02:48.506: D/dalvikvm(531): VFY: replacing opcode 0x60 at 0x0003
03-12 15:02:48.506: D/dalvikvm(531): VFY: dead code 0x0005-0051 in Lcom/facebook/LoginActivity;.onCreate (Landroid/os/Bundle;)V
03-12 15:02:48.506: W/dalvikvm(531): VFY: unable to resolve static field 1413 (com_facebook_login_activity_progress_bar) in Lcom/facebook/android/R$id;
03-12 15:02:48.506: D/dalvikvm(531): VFY: replacing opcode 0x60 at 0x0008
03-12 15:02:48.506: D/dalvikvm(531): VFY: dead code 0x000a-0013 in Lcom/facebook/LoginActivity;.onPause ()V
03-12 15:02:48.596: W/dalvikvm(531): VFY: unable to resolve static field 1440 (com_facebook_internet_permission_error_title) in Lcom/facebook/android/R$string;
03-12 15:02:48.596: D/dalvikvm(531): VFY: replacing opcode 0x60 at 0x0010
03-12 15:02:48.606: D/dalvikvm(531): VFY: dead code 0x0012-0026 in Lcom/facebook/AuthorizationClient;.checkInternetPermission ()Z
03-12 15:02:48.716: W/dalvikvm(531): VFY: unable to resolve static field 1407 (com_facebook_close) in Lcom/facebook/android/R$drawable;
03-12 15:02:48.716: D/dalvikvm(531): VFY: replacing opcode 0x60 at 0x001d
03-12 15:02:48.716: D/dalvikvm(531): VFY: dead code 0x001f-002e in Lcom/facebook/widget/WebDialog;.createCrossImage ()V
03-12 15:02:48.726: W/dalvikvm(531): VFY: unable to resolve static field 1441 (com_facebook_loading) in Lcom/facebook/android/R$string;
03-12 15:02:48.726: D/dalvikvm(531): VFY: replacing opcode 0x60 at 0x0024
03-12 15:02:48.736: D/dalvikvm(531): VFY: dead code 0x0026-006d in Lcom/facebook/widget/WebDialog;.onCreate (Landroid/os/Bundle;)V
03-12 15:02:48.746: D/AndroidRuntime(531): Shutting down VM
03-12 15:02:48.746: W/dalvikvm(531): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-12 15:02:48.756: E/AndroidRuntime(531): FATAL EXCEPTION: main
03-12 15:02:48.756: E/AndroidRuntime(531): java.lang.NoClassDefFoundError: com.facebook.android.R$string
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.facebook.widget.WebDialog.onCreate(WebDialog.java:188)
03-12 15:02:48.756: E/AndroidRuntime(531):  at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
03-12 15:02:48.756: E/AndroidRuntime(531):  at android.app.Dialog.show(Dialog.java:225)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.facebook.AuthorizationClient$WebViewAuthHandler.tryAuthorize(AuthorizationClient.java:461)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.facebook.AuthorizationClient.tryCurrentHandler(AuthorizationClient.java:209)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.facebook.AuthorizationClient.tryNextHandler(AuthorizationClient.java:188)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.facebook.AuthorizationClient.authorize(AuthorizationClient.java:116)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.facebook.AuthorizationClient.startOrContinueAuth(AuthorizationClient.java:97)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.facebook.Session.tryLegacyAuth(Session.java:1096)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.facebook.Session.authorize(Session.java:893)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.facebook.Session.open(Session.java:957)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.facebook.Session.openForPublish(Session.java:415)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.facebook.android.Facebook.openSession(Facebook.java:319)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.facebook.android.Facebook.authorize(Facebook.java:313)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.facebook.android.Facebook.authorize(Facebook.java:149)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.example.kumarfbapp.FBActivity.loginToFacebook(FBActivity.java:62)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.example.kumarfbapp.FBActivity$1.onClick(FBActivity.java:41)
03-12 15:02:48.756: E/AndroidRuntime(531):  at android.view.View.performClick(View.java:2408)
03-12 15:02:48.756: E/AndroidRuntime(531):  at android.view.View$PerformClick.run(View.java:8816)
03-12 15:02:48.756: E/AndroidRuntime(531):  at android.os.Handler.handleCallback(Handler.java:587)
03-12 15:02:48.756: E/AndroidRuntime(531):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 15:02:48.756: E/AndroidRuntime(531):  at android.os.Looper.loop(Looper.java:123)
03-12 15:02:48.756: E/AndroidRuntime(531):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-12 15:02:48.756: E/AndroidRuntime(531):  at java.lang.reflect.Method.invokeNative(Native Method)
03-12 15:02:48.756: E/AndroidRuntime(531):  at java.lang.reflect.Method.invoke(Method.java:521)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-12 15:02:48.756: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-12 15:02:48.756: E/AndroidRuntime(531):  at dalvik.system.NativeStart.main(Native Method)
03-12 15:02:52.136: I/Process(531): Sending signal. PID: 531 SIG: 9
03-12 15:03:36.066: W/com.facebook.Session(561): Should not pass a read permission (email) to a request for publish or manage authorization
03-12 15:03:36.086: W/dalvikvm(561): VFY: unable to resolve static field 1431 (com_facebook_login_activity_layout) in Lcom/facebook/android/R$layout;
03-12 15:03:36.086: D/dalvikvm(561): VFY: replacing opcode 0x60 at 0x0003
03-12 15:03:36.086: D/dalvikvm(561): VFY: dead code 0x0005-0051 in Lcom/facebook/LoginActivity;.onCreate (Landroid/os/Bundle;)V
03-12 15:03:36.086: W/dalvikvm(561): VFY: unable to resolve static field 1413 (com_facebook_login_activity_progress_bar) in Lcom/facebook/android/R$id;
03-12 15:03:36.086: D/dalvikvm(561): VFY: replacing opcode 0x60 at 0x0008
03-12 15:03:36.086: D/dalvikvm(561): VFY: dead code 0x000a-0013 in Lcom/facebook/LoginActivity;.onPause ()V
03-12 15:03:36.136: W/dalvikvm(561): VFY: unable to resolve static field 1440 (com_facebook_internet_permission_error_title) in Lcom/facebook/android/R$string;
03-12 15:03:36.136: D/dalvikvm(561): VFY: replacing opcode 0x60 at 0x0010
03-12 15:03:36.136: D/dalvikvm(561): VFY: dead code 0x0012-0026 in Lcom/facebook/AuthorizationClient;.checkInternetPermission ()Z
03-12 15:03:36.226: W/dalvikvm(561): VFY: unable to resolve static field 1407 (com_facebook_close) in Lcom/facebook/android/R$drawable;
03-12 15:03:36.226: D/dalvikvm(561): VFY: replacing opcode 0x60 at 0x001d
03-12 15:03:36.226: D/dalvikvm(561): VFY: dead code 0x001f-002e in Lcom/facebook/widget/WebDialog;.createCrossImage ()V
03-12 15:03:36.246: W/dalvikvm(561): VFY: unable to resolve static field 1441 (com_facebook_loading) in Lcom/facebook/android/R$string;
03-12 15:03:36.246: D/dalvikvm(561): VFY: replacing opcode 0x60 at 0x0024
03-12 15:03:36.246: D/dalvikvm(561): VFY: dead code 0x0026-006d in Lcom/facebook/widget/WebDialog;.onCreate (Landroid/os/Bundle;)V
03-12 15:03:36.256: D/AndroidRuntime(561): Shutting down VM
03-12 15:03:36.256: W/dalvikvm(561): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-12 15:03:36.266: E/AndroidRuntime(561): FATAL EXCEPTION: main
03-12 15:03:36.266: E/AndroidRuntime(561): java.lang.NoClassDefFoundError: com.facebook.android.R$string
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.facebook.widget.WebDialog.onCreate(WebDialog.java:188)
03-12 15:03:36.266: E/AndroidRuntime(561):  at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
03-12 15:03:36.266: E/AndroidRuntime(561):  at android.app.Dialog.show(Dialog.java:225)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.facebook.AuthorizationClient$WebViewAuthHandler.tryAuthorize(AuthorizationClient.java:461)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.facebook.AuthorizationClient.tryCurrentHandler(AuthorizationClient.java:209)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.facebook.AuthorizationClient.tryNextHandler(AuthorizationClient.java:188)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.facebook.AuthorizationClient.authorize(AuthorizationClient.java:116)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.facebook.AuthorizationClient.startOrContinueAuth(AuthorizationClient.java:97)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.facebook.Session.tryLegacyAuth(Session.java:1096)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.facebook.Session.authorize(Session.java:893)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.facebook.Session.open(Session.java:957)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.facebook.Session.openForPublish(Session.java:415)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.facebook.android.Facebook.openSession(Facebook.java:319)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.facebook.android.Facebook.authorize(Facebook.java:313)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.facebook.android.Facebook.authorize(Facebook.java:149)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.example.kumarfbapp.FBActivity.loginToFacebook(FBActivity.java:64)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.example.kumarfbapp.FBActivity$1.onClick(FBActivity.java:43)
03-12 15:03:36.266: E/AndroidRuntime(561):  at android.view.View.performClick(View.java:2408)
03-12 15:03:36.266: E/AndroidRuntime(561):  at android.view.View$PerformClick.run(View.java:8816)
03-12 15:03:36.266: E/AndroidRuntime(561):  at android.os.Handler.handleCallback(Handler.java:587)
03-12 15:03:36.266: E/AndroidRuntime(561):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 15:03:36.266: E/AndroidRuntime(561):  at android.os.Looper.loop(Looper.java:123)
03-12 15:03:36.266: E/AndroidRuntime(561):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-12 15:03:36.266: E/AndroidRuntime(561):  at java.lang.reflect.Method.invokeNative(Native Method)
03-12 15:03:36.266: E/AndroidRuntime(561):  at java.lang.reflect.Method.invoke(Method.java:521)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-12 15:03:36.266: E/AndroidRuntime(561):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-12 15:03:36.266: E/AndroidRuntime(561):  at dalvik.system.NativeStart.main(Native Method)
0

3-12 15:03:38.336:I / Process(561):发送信号。 PID:561 SIG:9

请帮帮我。

1 个答案:

答案 0 :(得分:1)

请完成此https://github.com/facebook/facebook-android-sdk/https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/

private OnClickListener loginButtonListener = new OnClickListener() {
    public void onClick(View v) {
        if (!mFacebook.isSessionValid()) {
            Toast.makeText(Login.this, "Authorizing", Toast.LENGTH_SHORT)
                    .show();
            mFacebook.authorize(Login.this, new String[] { "" },
                    new LoginDialogListener());
        } else {
            Toast.makeText(Login.this, "Has valid session",
                    Toast.LENGTH_SHORT).show();
            try {
                JSONObject json = Util.parseJson(mFacebook.request("me"));
                String facebookID = json.getString("id");
                String firstName = json.getString("first_name");
                String lastName = json.getString("last_name");
                Toast.makeText(
                        Login.this,
                        "You already have a valid session, " + firstName
                                + " " + lastName
                                + ". No need to re-authorize.",
                        Toast.LENGTH_SHORT).show();
            } catch (Exception error) {
                Toast.makeText(Login.this, error.toString(),
                        Toast.LENGTH_SHORT).show();
            } catch (FacebookError error) {
                Toast.makeText(Login.this, error.toString(),
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
};