2013-03-12 66 views
-2

我寫了一個代碼爲facebook integration.I創建應用程序Id並且還導入了Facebook sdk.I在我的主要活動上放置了一個按鈕,它將打開一個facebook對話框,詢問登錄ID和密碼。但點擊顯示「強制關閉」的AVD。裏面那個按鈕,我打電話以下功能:如何在我的android應用程序中整合Facebook?

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 /處理(561):發送信號。 PID:561 SIG:9

請幫幫我。

+0

還請張貼'logcat'信息。 – 2013-03-12 09:46:43

+0

@MayuMayooresan增加了logcat。 – 2013-03-12 10:11:47

+0

你看到有一個可讀的錯誤mesg'不應該通過讀取權限(電子郵件)發佈或管理授權請求 – 2013-03-12 10:19:21

回答

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

你可以告訴我在這裏傳遞什麼String [] {「」}, – 2013-03-12 10:21:10

+0

謝謝,我遵循了這些步驟。它的工作。 – 2013-03-12 11:25:12

+0

Swapnil可以請你引導我爲facebook 3.0相同。我看過教程,但他們正在使用片段活動並且有點複雜。我可以登錄Facebook,但需要知道如何從Facebook獲取用戶數據。你能指導我通過任何簡化的教程。謝謝! – nidhi 2013-04-11 09:19:24

相關問題