Java – Sign in with Facebook on Android

Sign in with Facebook on Android… here is a solution to the problem.

Sign in with Facebook on Android

I’m trying to add the “Sign in with Facebook” feature to my app, but I’m having the following issue

  1. When I add the Facebook login button to the XML, there is a rendering issue (the problem disappears when I refresh the layout, but again I get a white screen like this)
    Code:
    <TextView
    android:id="@+id/btnLogin"
    android:layout_width="wrap_content"
    android:layout_height="31dp"
    android:layout_gravity="center"
    android:layout_marginLeft="175dp"
    android:text="Login With Facebook"
    android:backgroundTint="#948b8b" />
    <com.facebook.login.widget.LoginButton
    android:id="@+id/loginButton"
    android:visibility="gone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    </LinearLayout>

Screen Rendering

  1. When I try to run my application, I see the following stack trace information in the console.

I’m new to Android and can’t fix the problem. Thank you very much for your help.
Code:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if(PrefUtils.getCurrentUser(LoginActivity.this) != null){
Intent homeIntent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(homeIntent);
finish();
}
}

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.batman.eventmanager, PID: 2111 java.lang.ExceptionInInitializerError at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:288) at android.view.LayoutInflater.createView(LayoutInflater.java:607) at android.view.LayoutInflater. createViewFromTag(LayoutInflater.java:743) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at android.view.LayoutInflater.inflate(LayoutInflater.java: 365) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378) at android.app.Activity.setContentView(Activity.java:2145) at com.example.batman. eventmanager. LoginActivity.onCreate(LoginActivity.java:33) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread. handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java: 102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit. main(ZygoteInit.java:698) Caused by: null at com.facebook.internal.Validate.sdkInitialized(Validate.java:99) at com.facebook.FacebookSdk. getCallbackRequestCodeOffset(FacebookSdk.java:735) at com.facebook.internal.CallbackManagerImpl$RequestCodeOffset. toRequestCode(CallbackManagerImpl.java:109) at com.facebook.login.widget.LoginButton.<clinit>(LoginButton.java:58) at java.lang.reflect.Constructor.newInstance(Native Method)  at java.lang.reflect.Constructor.newInstance(Constructor.java:288)  at android.view.LayoutInflater. createView(LayoutInflater.java:607)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)  at android.view.LayoutInflater.inflate(LayoutInflater. java:414)  at android.view.LayoutInflater.inflate(LayoutInflater.java:365)  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)  at android.app.Activity.setContentView(Activity.java:2145)  at com.example.batman.eventmanager.LoginActivity. onCreate(LoginActivity.java:33)  at android.app.Activity.performCreate(Activity.java:5990)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)  at android.app.ActivityThread. handleLaunchActivity(ActivityThread.java:2387 at android.app.ActivityThread.access$800(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5254)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  at com.android.internal.os.ZygoteInit. main(ZygoteInit.java:698)

  1. Below is my list XML
    `

    <activity
        android:name=". LoginActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
    <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
    <activity
        android:name=". MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="com.example.batman.eventmanager.MainActivity" />
    
    <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <uses-permission android:name="android.permission.INTERNET"/>
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
    
    <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" />
    

    `
    TIA

Solution

You need to initialize the Facebook SDK before

setContentView().

Place the following code before setContentView();

       FacebookSdk.sdkInitialize(getApplicationContext());
       callbackManager = CallbackManager.Factory.create();

Related Problems and Solutions