New Facebook API 3.0. and ActionBarSherlock compatibility

拈花ヽ惹草 提交于 2019-12-31 19:54:13

问题


I'm reading facebook Android API 3.0 documents, and I do not understand what does session has to do with background activities. In all examples I'm supposed to extend "FacebookFragment". Well, that would be nice if my whole app is not extending "SherlockFragment" so I do not see extending FacebookFragment as an option.

If I use the code from SessionLoginExample, and put my AppID in Strings in the very same string as facebook does I get the:

Error

10-22 17:04:26.464: E/AndroidRuntime(5491): FATAL EXCEPTION: main
10-22 17:04:26.464: E/AndroidRuntime(5491): java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.specsavers.moodspecs/nl.specsavers.moodspecs.LauncherActivity}: java.lang.NullPointerException: Argument applicationId cannot be null

I'm trying to set up the initial check if the app is installed or not, and if session exists or not.

 Settings.addLoggingBehavior(LoggingBehaviors.INCLUDE_ACCESS_TOKENS);

    Session session = Session.getActiveSession();
    if (session == null) {
        if (savedInstanceState != null) {
            session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
        }
        if (session == null) {
            session = new Session(this);
        }
        Session.setActiveSession(session);
        if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
        }

    } 

I need to be able to somehow put my App ID somewhere that it is actually OK, but I can't find where to put it.

I need to know If I can avoid extending FacebookFragment as I use SherlockFragment.

Tnx.


回答1:


FacebookFragment is not public and is not intended for use by applications.

The samples do mostly use FacebookActivity though, and yes--you do not need to extend FacebookActivity. As you mention, SessionLoginSample demonstrates this, and if you are not using FacebookActivity you should handle Session serialization and override/forward onActivityResult as illustrated there.

That said, it sounds like the main issue you are hitting is around setting the applicationId. You can set applicationId in code or in meta-data.

In code:

To set it in code, define a String constant MY_APP_ID that contains your app id and replace the line:

session = new Session(this);

with:

session = new Session.Builder(this).setApplicationId(MY_APP_ID).build();

In metadata:

To set it in meta-data, add the following to your AndroidManifest.xml inside the application tag but outside of any activity tags:

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

Then, in values/strings.xml, add a value for app_id that has your app id:

<string name="app_id">1234567890</string>



回答2:


You do not need to inherit from anything, if you don't provide an app_id through code, you must specify it in your application metadata (and in your strings.xml). See https://developers.facebook.com/docs/reference/android/3.0/Session#APPLICATION_ID_PROPERTY

In your AndroidManifest.xml, you need to add:

<application android:label="@string/app_name" ...>
  ... other activity stuff here ...
  <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
</application>

And then add app_id as a string in your string.xml.



来源:https://stackoverflow.com/questions/13014938/new-facebook-api-3-0-and-actionbarsherlock-compatibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!