2013-05-14 75 views
0

我想在這裏按照facebook教程https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/,但我得到onActivityResult錯誤任何想法?我和他們之間的唯一區別是我的是一個片段。Android的Facebook SDK與片段

package com.projectcaruso.naturalfamilyplaning; 

    import com.facebook.*; 
    import com.facebook.model.*; 
    import com.projectcaruso.naturalfamilyplanning.R; 

    import android.os.Bundle; 
    import android.support.v4.app.Fragment; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.TextView; 
    import android.widget.Toast; 
    import android.content.Context; 
    import android.content.Intent; 

    public class facebooklogin extends Fragment{ 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
     }  

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View view = inflater.inflate(R.layout.fragment_login, container, false); 

      // start Facebook Login 
      Session.openActiveSession(getActivity(), this, true, new Session.StatusCallback() { 

       // callback when session changes state 
       @Override 
       public void call(Session session, SessionState state, Exception exception) { 
       if (session.isOpened()) { 

        // make request to the /me API 
        Request.executeMeRequestAsync(session, new Request.GraphUserCallback() { 

        // callback after Graph API response with user object 
        @Override 
        public void onCompleted(GraphUser user, Response response) { 
         if (user != null) { 
          Context context = getActivity(); 
          Toast.makeText(context, "Hello " + user.getName() + "!", Toast.LENGTH_LONG).show(); 
         } 
        } 
        }); 
       } 
       } 
      }); 

      return view; 
      } 

      @Override 
      public void onActivityResult(int requestCode, int resultCode, Intent data) { 
       super.onActivityResult(requestCode, resultCode, data); 
       Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 
      } 

    } 

編輯: 我改變這getActivity(),但現在我得到這個

05-14 17:10:23.904: E/AndroidRuntime(791): FATAL EXCEPTION: main 
05-14 17:10:23.904: E/AndroidRuntime(791): java.lang.NullPointerException: Argument 'applicationId' cannot be null 
05-14 17:10:23.904: E/AndroidRuntime(791): at com.facebook.internal.Validate.notNull(Validate.java:29) 
05-14 17:10:23.904: E/AndroidRuntime(791): at com.facebook.Session.<init>(Session.java:224) 
05-14 17:10:23.904: E/AndroidRuntime(791): at com.facebook.Session.<init>(Session.java:213) 
05-14 17:10:23.904: E/AndroidRuntime(791): at com.facebook.Session$Builder.build(Session.java:1454) 
05-14 17:10:23.904: E/AndroidRuntime(791): at com.facebook.Session.openActiveSession(Session.java:863) 
05-14 17:10:23.904: E/AndroidRuntime(791): at com.facebook.Session.openActiveSession(Session.java:828) 
05-14 17:10:23.904: E/AndroidRuntime(791): at com.projectcaruso.naturalfamilyplaning.facebookFragment.onCreateView(facebookFragment.java:29) 
05-14 17:10:23.904: E/AndroidRuntime(791): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460) 
05-14 17:10:23.904: E/AndroidRuntime(791): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 
05-14 17:10:23.904: E/AndroidRuntime(791): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 
05-14 17:10:23.904: E/AndroidRuntime(791): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
05-14 17:10:23.904: E/AndroidRuntime(791): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 
05-14 17:10:23.904: E/AndroidRuntime(791): at  android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429) 
05-14 17:10:23.904: E/AndroidRuntime(791): at android.os.Handler.handleCallback(Handler.java:725) 
05-14 17:10:23.904: E/AndroidRuntime(791): at android.os.Handler.dispatchMessage(Handler.java:92) 
05-14 17:10:23.904: E/AndroidRuntime(791): at android.os.Looper.loop(Looper.java:137) 
05-14 17:10:23.904: E/AndroidRuntime(791): at android.app.ActivityThread.main(ActivityThread.java:5041) 
05-14 17:10:23.904: E/AndroidRuntime(791): at java.lang.reflect.Method.invokeNative(Native Method) 
05-14 17:10:23.904: E/AndroidRuntime(791): at java.lang.reflect.Method.invoke(Method.java:511) 
05-14 17:10:23.904: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
05-14 17:10:23.904: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
05-14 17:10:23.904: E/AndroidRuntime(791): at dalvik.system.NativeStart.main(Native Method) 

FYI這是我的表現,因此在我的字符串文件中的APP_ID。

meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id" 

清單:

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.projectcaruso.naturalfamilyplanning" 
     android:versionCode="1" 
     android:versionName="1.0" > 

     <uses-sdk 
      android:minSdkVersion="14" 
      android:targetSdkVersion="17" /> 

     <uses-permission android:name="android.permission.INTERNET"/> 
     <meta-data 
      android:name="com.facebook.sdk.ApplicationId" 
      android:value="@string/app_id"/> 

     <application 
      android:allowBackup="true" 
      android:icon="@drawable/ic_launcher" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme" > 
      <activity 
       android:name="com.projectcaruso.naturalfamilyplaning.FragmentChange" 
       android:configChanges="orientation|screenSize|screenLayout" 
       android:theme="@style/Theme.Sherlock.Light.DarkActionBar" 
       android:uiOptions="splitActionBarWhenNarrow" > 
       <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 

        <category android:name="android.intent.category.LAUNCHER" /> 
       </intent-filter> 
      </activity> 
      <activity android:name="com.projectcaruso.naturalfamilyplaning.UserSettingActivity" > 
      </activity> 
      <activity 
       android:name="com.projectcaruso.naturalfamilyplaning.HistoryFragment" 
       android:label="@string/title_activity_history_fragment" > 
      </activity> 
     </application> 

    </manifest> 
+1

什麼錯誤? – 2013-05-14 17:03:33

+0

我在更改之後立即收到運行時錯誤...請參閱上面的 – jcaruso 2013-05-14 17:14:33

+0

您是否遵循指南中的步驟6,它要求您將應用程序標識放在strings.xml和AndroidManifest.xml中的元數據字段中? – 2013-05-14 17:32:17

回答

2

你必須在你的清單放錯了地方的標籤。

它應該位於<Application>元素內,但您不在該元素之外。

它應該是:

<application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
    <meta-data 
     android:name="com.facebook.sdk.ApplicationId" 
     android:value="@string/app_id"/> 
    .... 
</application> 
相關問題