0

I wanted to add the login button to my app and so i followed the guide of facebook about that. I created my app project i add an android project telling the name of the package and the path of the mainactivity and the hash code generated from the default keystore (i used as they tell to me the android debug keystore because for the moment the app is only in alfa test and so for personal developing reason i prefer to use the debug version generated from android studio). Then i put in string resource the app id and in the tag application the meta-data with facebook app id as the guide says. Then i set the enviroment as well:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());

    /**
     *
     *
     * part dedicated to facebook button
     */
    manager=CallbackManager.Factory.create();

    setContentView(R.layout.activity_main);
    loginButton=(LoginButton)findViewById(R.id.facebook_logi);
    loginButton.setReadPermissions("email");

    loginButton.registerCallback(manager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.i("Facebook","Login success");
        }

        @Override
        public void onCancel() {
            Log.i("Facebook","Login cencelled");
        }

        @Override
        public void onError(FacebookException error) {
            Log.i("Facebook","Login error "+error.getMessage());

        }
    });

    /**
     *
     *
     * end of facebook part
     */

and when i tried to run it the VM generate this exception

 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
                                                                                      at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746)
                                                                                      at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:774)
                                                                                      at com.facebook.internal.FetchedAppSettingsManager.queryAppSettings(FetchedAppSettingsManager.java:147)
                                                                                      at com.facebook.login.widget.LoginButton$1.run(LoginButton.java:499)
                                                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                      at java.lang.Thread.run(Thread.java:818)

can anyone help me becouse i searched here for similar questions but i only found ones where people putted the meda-data with facebook_app_id outsidethe application tag..Thanks to all indeed.

P.Carlino
  • 661
  • 5
  • 21
  • I'm assuming you meant to call `R.id.facebook_login` not `R.id.facebook_logi` which is return a `npe`. Either way, read this http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Pztar Jan 25 '17 at 15:00
  • what is `R.id.facebook_logi` you custom login button? – rafsanahmad007 Jan 25 '17 at 19:10
  • Is the id of facebookloginbutton putted in te xml file. – P.Carlino Jan 25 '17 at 19:13

1 Answers1

0

your meta tag should be inside application tag in Manifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

</application>

Also if u use custom login button;

add this to your layout:

 <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

And in your activity

 loginButton=(LoginButton)findViewById(R.id.login_button);
loginButton.setReadPermissions("email");

loginButton.registerCallback(manager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        Log.i("Facebook","Login success");
    }

    @Override
    public void onCancel() {
        Log.i("Facebook","Login cencelled");
    }

    @Override
    public void onError(FacebookException error) {
        Log.i("Facebook","Login error "+error.getMessage());

    }
});

now just invoke facebook login button when user clicked on your custom button

public void onClick(View v) {
    if (v == facebook_logi) {
        loginButton.performClick();
    }
 }
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • and i've done it... is that the problem... the error i understood is with the app id that is not detected and all works if a call FacebookSdk.setApplicationId(string id) – P.Carlino Jan 26 '17 at 13:55
  • In your case the custom login button does not initialize facebook callback ..and so cause the exception – rafsanahmad007 Jan 26 '17 at 14:27
  • This is the problem. It doesn't work if I put it into the manifest – P.Carlino Jan 26 '17 at 14:27