Problem:
In the very first Facebook SDK tutorial, when I finish the final steps to get a MainActivity.java that looks like the tutorial, instead of getting the message "HelloMy code looks exactly like the tutorial found at https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/. (For reference, the tutorial code as of September 6, 2013 is below, as it could be revised by now:)
package com.firstandroidapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;
import com.facebook.*;
import com.facebook.model.*;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// start Facebook Login
Session.openActiveSession(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) {
TextView welcome = (TextView) findViewById(R.id.welcome);
welcome.setText("Hello " + user.getName() + "!");
}
}
});
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
}
Please Note First:
This problem and its workaround appears to be version specific. Your mileage may vary if the error isn't exactly the same as the one encountered. The versions of things used when this error was encountered were Android ADT v22, facebook-android-SDK-3.5, with a build target set to Android 4.2.2. All source code, tool, etc., were unmodified from the versions downloaded from Google and Facebook. This problem was worked around September 6, 2013, so hopefully someone from Facebook or Google's Android team has already made the appropriate fixes (making this post no longer needed =p).
Workaround:
This problem is caused by a missing method that the Facebook SDK is expecting to find in android.support.v4, but cannot find in the current version of the Android libraries. A hint to this is a strange error (which you may or may not have seen when importing the Facebook SDK demos into Eclipse) mentions that the versions of android.support.v4 do not match. The demos seem to run properly when compiled, however the very first Facebook SDK program you write yourself does not.
To work around this problem, the Facebook SDK version 3.5 includes a jar file you need to link to your project, located in "/path/to/facebook-android-sdk-3.5/libs/android-support-v4.jar".
One way to do this is as follows:
- Go to your project's properties in Eclipse, then go to "Java Build Path"
- Click on the "Libraries" tab
- Click on either "Add JARs..." or "Add External JARs...", depending on which of the two you'd like to do
- Add the android-support-v4.jar that comes with the Facebook Android SDK v3.5
- Click on the "Order and Export" tab
- Check the newly-imported android-support-v4.jar
- Clean the project (Project->Clean...) (this step is important as attempting to re-run the project without cleaning it first or altering its code will simply run the existing binary again, which will give the error once more)
Additional Notes:
There's also another change in the tutorial which you may want to make. The function "Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {....});" is deprecated in the v3.5 SDK. You can get rid of the "deprecated method" warning in Eclipse by changing the above code to "Request.newMeRequest(session, new Request.GraphUserCallback() {...}).executeAsync();".
Hopefully this post helped someone out there. I realize it's a pretty basic problem to debug for experienced coders, but I decided to post this anyway in case it came in handy.
Hey
ReplyDeleteI did the changes as you mentioned. However, I get the final page saying
"Hello World" rather than "Hello ".
Do you have any idea why it happens?
Thanks for the comment Kathan! I can't be sure without seeing your code, but I know that I've looked over IDs in XML layouts before before. Does the text view in your activity_main.xml have something like:
ReplyDelete<TextView
android:id="@+id/welcome"
...
Something like this is needed because the code snippet in the Facebook tutorial and this blog post retrieves the text view that it'll change by: TextView welcome = (TextView) findViewById(R.id.welcome);
Hope this helped! (And my apologies if it was totally irrelevant!)
Hello
ReplyDeletewhen i click on login button , it takes some minutes , accept permission then it redirect me to the same page :/
Hello ,when I tried log in button, it just returned to log in screen ? can you help me
ReplyDeleteHi Damoh, sorry to hear that the Facebook Android tutorial seems to be acting up on you. I'm afraid that from the description you provided of "when I tried log in button, it just returned to log in screen ?", I'm not sure what could be the bug, as the code on the tutorial page (as of today, October 28, 2013) doesn't have a log in button (currently looking at "A minimum viable social application" at https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/). Also, the question was a bit out of the scope of this post, which was only to solve the error for "Could not find method android.support.v4.content.LocalBroadcastManager.getInstance, referenced from method com.facebook.Session.postActiveSessionAction". I hope that you have luck debugging the problem you ran into, and sorry that I couldn't provide more help :/
ReplyDeleteHi again Damoh, now that I think about your questions, it's possible that you might just have to re-launch the "minimum viable social application" from this step of the tutorial after Facebook is logged in. The login screen is either the Facebook app or their website (depending on if the app is installed), which could possibly not return you back to your own app in some cases (I've run into those too, and I'm not sure why). The "hello " in this app will only show up if your Android is logged into Facebook.
ReplyDeleteI'm not 100% sure if this was what you were asking with your questions "Hello
when i click on login button , it takes some minutes , accept permission then it redirect me to the same page :/" and "Hello ,when I tried log in button, it just returned to log in screen ?", but I hope that it was at least somewhat close, and that this comment helped and was not totally off. Regardless, I wish you the best of luck with the tutorial!
hello wcdev,
ReplyDeletethanks for the solution. I have been struggling with this problem since the last 5 hour and, after searching online, finally got it resolved. There were many other posts about it on stackoverflow, but none gave a step by step instruction as yours.
thanks a lot again!
I know I'm way late but I Facebook is still having the same problem and your blog was the fix. Thanks for the post!!!
ReplyDeletethanks for the comment @Dustin Finamore! I'm glad it helped (and a little surprised the problem still exists!)
ReplyDeletethanks for the comment @gautam. Glad it helped!
ReplyDelete