Android facebook api - android

i'm just trying to use the facebook api in my android application, my code is :
public class FacebookActivity extends AppCompatActivity{
CallbackManager callbackManager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_facebook);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
I think this is the base but i don't know how to do it properly.
I have a button to connect to my facebook accompte, but in my method onSucces, i don't know what i have to put in.
My second question is about request. I want to integrate a friend request in this same app but i don't found any informations if someone can help me.
Thanks you

<com.facebook.login.widget.LoginButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
facebook:com_facebook_confirm_logout="false"
facebook:com_facebook_tooltip_mode="never_display"/>
Used this in your xml file

Related

why setReadPermissions is rendering NullPointerException in Facebook login in android studio

i am trying to integrate Facebook login into my app but when i try to enter to the activity when my button is.
I followed this tutorial: http://alvarez.tech/login-facebook-en-android/
my activity is like:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_THIS=this;
setContentView(R.layout.connexion_layout);
if(savedInstanceState!= null) return;
FrameLayout frameLayout = (FrameLayout)findViewById(R.id.fragment_container);
connexion0 = new connexion0Fragment();
connexion1 = new connexion1Fragment();
changeFragment(0);
connexionViaFacebook();
}
public void connexionViaFacebook(){
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.loginButton);
loginButton.setReadPermissions(Arrays.asList("email"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
goMainScreen();
}
#Override
public void onCancel() {
Toast.makeText(getApplicationContext(), R.string.cancel_login, Toast.LENGTH_SHORT).show();
}
#Override
public void onError(FacebookException error) {
Toast.makeText(getApplicationContext(), R.string.error_login, Toast.LENGTH_SHORT).show();
}
});
}
the error is:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.login.widget.LoginButton.setReadPermissions(java.util.List)' on a null object reference
I think you have forgot to add the facebook initialization code in the oncreate() method of your main activity. FacebookSdk.sdkInitialize(getApplicationContext());
In your reference link, the initialization part has been done in the class extending Application.
Reference:
facebook android integration - Getting started
Setting up fb integration - with pictures

Facebook login button crash

I did all steps from Android Facebook SDK 4 in Eclipse and https://developers.facebook.com/docs/android/getting-started#login_share
My activity:
public class MainActivity extends Activity {
LoginButton loginButton;
CallbackManager callbackManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
final Button but_nova_hra = (Button) findViewById(R.id.nova_hra);
but_nova_hra.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent obrazovkaHry = new Intent(getApplicationContext(), HraActivity.class);
startActivity(obrazovkaHry);
}
});
final Button but_koniec = (Button) findViewById(R.id.konec);
but_koniec.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
App crashes during start with
Could not find com.facebook.FacebookActivity referenced from method com.facebook.internal.Validate.hasFacebookActivity
Where can the problem be?
EDIT: Just created new blank app with completly same settings and FacebookSdk.sdkInitializing passed sucessfully. There have to be some problem with this specific application, but where ? Both of them have same settings ?
I guess you need to declare FacebookActivity in your manifest file like this
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
android:label="#string/app_name" />
Its mentioned here..
LoL, I just solved my problem. In the activity layout needs to be
tools:context="com.kajmus.appname.MainActivity"

Android facebook Login button crash

I did all steps from Android Facebook SDK 4 in Eclipse and https://developers.facebook.com/docs/android/getting-started#login_share
My activity:
public class MainActivity extends Activity {
LoginButton loginButton;
CallbackManager callbackManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
final Button but_nova_hra = (Button) findViewById(R.id.nova_hra);
but_nova_hra.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent obrazovkaHry = new Intent(getApplicationContext(), HraActivity.class);
startActivity(obrazovkaHry);
}
});
final Button but_koniec = (Button) findViewById(R.id.konec);
but_koniec.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
App crashes during start with
Could not find com.facebook.FacebookActivity referenced from method com.facebook.internal.Validate.hasFacebookActivity
Where can the problem be?
the answer of that error is initialize the facebook SDK line before the set content view after the oncreate constuctor

Facebook SDK for Android Cannot Resolve CallbackManager.registerCallback

I have been working to get Facebook SDK 4.0.1 running on Android but I have not been able to get the LoginButton to cause a login screen to open.
In the XML for my screen I have:
<com.facebook.login.widget.LoginButton
android:id="#+id/myLoginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
/>
Then in my Activity I have:
CallbackManager callbackManager;
LoginButton loginButton;
#Override
public void onCreate(Bundle savedInstanceState) {
MainActivity.localyticsSession.tagEvent("Sign In");
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.app_user_loginactivity);
callbackManager = CallbackManager.Factory.create();
// Initialize the Facebook LoginButton
loginButton = (LoginButton) findViewById(R.id.myLoginButton);
//loginButton.setBackgroundColor(Color.RED); // this works
loginButton.setReadPermissions("public_profile");
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.i("LoginActivity","FB Login Success");
}
#Override
public void onCancel() {
// App code
Log.i("LoginActivity","FB Login Cancel");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.i("LoginActivity","FB Login Error");
}
});
}
When I tap the LoginButton it blinks like it is reacting, but the login screen does not appear.
Update
Android Studio is giving me a message that says Cannot resolve method registerCallback. I looked in com.facebook.CallbackManager which then imports com.facebook.internal.CallbackManagerImpl
CallbackManagerImpl.java contains this method:
public void registerCallback(int requestCode, Callback callback) {
Validate.notNull(callback, "callback");
callbacks.put(requestCode, callback);
}
I added callbackManager = CallbackManager.Factory.create(); to my code, but that is not making a difference.
where is the "onClick()" implementation for the FB logon button?
if its in the widget class then you need to drill-down into what is being called on the button click event....
below is sample from a more complex example (git parselogin-ui) but it gives the idea of what u are missing...
facebookLoginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
loadingStart(false); // Facebook login pop-up already has a spinner
if (config.isFacebookLoginNeedPublishPermissions()) {
ParseFacebookUtils.logInWithPublishPermissionsInBackground(getActivity(),
config.getFacebookLoginPermissions(), facebookLoginCallbackV4);
} else {
ParseFacebookUtils.logInWithReadPermissionsInBackground(getActivity(),
config.getFacebookLoginPermissions(), facebookLoginCallbackV4);
}
}
});
see here #292

Facebook android api v4.0 callback is working wrongly

I am using Android Facebook API 4.0. When I click the cancel button on share window, onSuccess() function is triggered but onCancel() should be triggered. Is this a bug? Please help me find a solution.
CallbackManager callbackManager;
ShareDialog shareDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(){
// this function is calling when I Click cancel button
}
#Override
public void onCancel(){
}
#Override
public void onError(){
}
});
}

Categories

Resources