0

我正在尝试遵循此处的 facebook 教程https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/但我在 onActivityResult 有任何想法吗?我的和他们的唯一区别就是我的是一个碎片。

    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)

仅供参考,这在我的清单中,我的字符串文件中的 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>
4

1 回答 1

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>
于 2013-05-15T14:54:34.233 回答