Skip to content Skip to sidebar Skip to footer

Facebook Login Is Not Working In My Android App

I am trying to implement facebook login in my app but whenever i launch my app it crashes. I am following facebook developer site. Here is my code. MainActivity.java package com.a

Solution 1:

Instead of

loginButton.findViewById(R.id.login_button);

Use

findViewById(R.id.login_button);

The problem is

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.facebook.login.widget.LoginButton.findViewById(int)'on a nullobject reference
    at com.algor7.samplelogin.MainActivity.onCreate(MainActivity.java:54)

What means that loginButton is null. You are trying to find the view login_button inside loginButton which hasn't been initialized yet. You have to find the view inside the activity instead.

Solution 2:

Try to do like this:





publicclassMainActivityextendsActivity {
     private CallbackManager mfbCallbackManager;
    private LoginManager mfbLoginManager;
    Override
        protectedvoidonCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            FacebookSdk.sdkInitialize(getApplicationContext());
            setContentView(R.layout. activity_main);
    mfbCallbackManager = CallbackManager.Factory.create();
            mfbLoginManager = LoginManager.getInstance();
            mfbLoginManager.registerCallback(mfbCallbackManager, newFacebookCallback<LoginResult>() {

                @OverridepublicvoidonSuccess(LoginResult fbLoginResult) {

                    GraphRequestfbGraphRequest= GraphRequest.newMeRequest(fbLoginResult.getAccessToken(), newGraphRequest.GraphJSONObjectCallback() {

                        @OverridepublicvoidonCompleted(JSONObject object, GraphResponse response) {
                            ProfilefbProfile= Profile.getCurrentProfile();
                            Stringemail=null, gender = null, dob = null;
                            try {
                                email = object.getString("email");
                                gender = object.getString("gender");
                                dob = object.getString("birthday");
                            } catch (JSONException e) {
                                AppUtil.writeLog(TAG, e.getMessage());
                            }
                            mCustomer = newCustomer();
                            try {
                                mCustomer.setCustFbId(fbProfile.getId());
                                mCustomer.setCustName(fbProfile.getName());
                                mCustomer.setCustFbiImageUrl(fbProfile.getProfilePictureUri(Constants.FB_PROFILE_PIC_WIDTH, Constants.FB_PROFILE_PIC_HEIGHT).toString());
                                mCustomer.setCustEmail(email);
                                mCustomer.setCustGender(gender);
                                mCustomer.setCustType(Constants.CUSTOMER_ROLE);
                                mCustomer.setCustDob(dob);
                            } catch (NullPointerException e) {
                                AppUtil.writeLog(TAG, e.getMessage());
                            }

                            loginWithFb();
                        }
                    });

                    Bundleparameters=newBundle();
                    parameters.putString("fields", "gender, age_range, email, birthday");
                    fbGraphRequest.setParameters(parameters);
                    fbGraphRequest.executeAsync();

    }

    privatevoidloginWithFb() {
            EndpointInterfaceapiService= AppUtil.getRetrofitInstance().create(EndpointInterface.class);
            Call<LoginResp> call = apiService.loinWithFacebook(
                    mCustomer.getCustFbId(),
                    mCustomer.getCustName(),
                    mCustomer.getCustType(),
                    mCustomer.getCustFbiImageUrl(),
                    mCustomer.getCustEmail(),
                    mCustomer.getCustDob(),
                    mCustomer.getCustGender(),
                    mAppPreferences.getString(Constants.SETTINGS_GCM_ID, ""),
                    IS_PUSH_ENABLED
            );

final ProgressDialog dialog;
        dialog = newProgressDialog(LoginActivity.this);
        dialog.setMessage(getString(R.string.fb_login_wait_msg));
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        call.enqueue(newCallback<LoginResp>() {
            @OverridepublicvoidonResponse(Call<LoginResp> call, Response<LoginResp> response) {
                mCustomer.setCustId(response.body().getCustId());
                mCustomer.setIsNew(response.body().getIsNew());

                SharedPreferences.EditorpreferenceEditor= mAppPreferences.edit();
                preferenceEditor.putString(Constants.SETTINGS_OBJ_CUSTOMER, newGson().toJson(mCustomer));
                preferenceEditor.putBoolean(Constants.SETTINGS_IS_LOGGED_IN, true);
                preferenceEditor.apply();

                dialog.dismiss();
                startActivity(newIntent(LoginActivity.this, HomeActivity.class));
                finish();
            }

            @OverridepublicvoidonFailure(Call<LoginResp> call, Throwable t) {
                dialog.dismiss();
                Toast.makeText(LoginActivity.this, R.string.server_error + t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }

Solution 3:

You are supposed to do the login in a fragment rather than in an activity . If i'm not wrong , facebook doc asks you to place the login button in a fragment. Then set up the button in your UI by adding it to a fragment and update your activity to use your fragment.

Post a Comment for "Facebook Login Is Not Working In My Android App"