0

Trying to deploy FB Login in Android Application (using Android Studio) Unable to render Facebook Login Button, We followed the FB official documentation for this task

Following is our login button code

MainActivity.java

public class MainActivity extends Activity {
    CallbackManager callbackManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_main);

        FacebookSdk.sdkInitialize(this.getApplicationContext());

        callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().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
                    }
                });


    }





}

MainActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">



    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp" />

</RelativeLayout>

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.dotweb.fbconnect" >


    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"


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

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
//meta data was here
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Build Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "in.dotweb.fbconnect"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.facebook.android:facebook-android-sdk:4.1.0'


}

We are encountering the following error when we compile

Could not instantiate com.facebook.login.widget.LoginButton

Screenshot of the error

We tried solutions specified in the following stackoverflow solutions but nothing is working

Link 1 Link 2

Community
  • 1
  • 1
Vikram
  • 817
  • 4
  • 22
  • 34
  • Where are you trying to instantiate the LoginButton? – Atul O Holic May 26 '15 at 12:04
  • when are you getting this error? is program executing or .. its crashes and give you this error? and did you try my solution i wrote below.? by putting this fb code into class extended with Fragment and .. loads SDK in your class extended with Application class? .. because it works so smoothly. – Ajay P. Prajapati May 26 '15 at 17:43

2 Answers2

1

The first step i want you to try is to, In android studio, go to the Project Structure and try to set your source compatibility and target compatibility same. This could be the problem.

if that doesn't solve the problem.follow these steps

second step: 1. add dependencies for fb 4.0.

add following code right before dependencies in your build.gradle(app) file.

repositories {

     mavenCentral()
}

in dependencies add this

compile 'com.facebook.android:facebook-android-sdk:4.0.0'

after this build the project.

  1. add this to your AndroidManifest.xml file

to add internet Permission :

<uses-permission android:name="android.permission.INTERNET" />

secondly... add following (Remember you should't have the same name as this class in your project. its for facebook internal purpose only.

<activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
       "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name" />

also add following for app id

 <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/APP_ID" />
  1. You don't need to put anything in your mainActivity class. create an any class which extends to Application class.

    public class SampleApplication extends Application {
    
    public void onCreate(){   
    FacebookSdk.sdkInitialize(getApplicationContext());
    printKeyHash();
    }
     public void printKeyHash() {
    try {
        PackageInfo info = getPackageManager().getPackageInfo("your package name", PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.e("Hash Key", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
        } catch (PackageManager.NameNotFoundException e) {
    
    } catch (NoSuchAlgorithmException e) {
    
    }
    }
    }
    

This code will initialize fb sdk and print hashKey in your logcat. I hope you know how to activate your app. edit: also don't forget to register this class in your AndroidManifest.xml file in your application tag.

  1. add following in your login.xml file to add button.

    <com.facebook.login.widget.LoginButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/login_button"
    android:layout_gravity="center_horizontal"
    
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
       />
    
  2. create a class extending fragment. where you can write all of your code related to facebook login.

I hope if you follow this steps your login button will work as you expect it to be. even if you don't create this 5th step.. your login button will appear without any error. if any problem let me know

Ajay P. Prajapati
  • 1,973
  • 1
  • 17
  • 34
0

Did you declare the FacebookActivity into you Manifest file, as well as the meta-data ?

Something like this:

 <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:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
Gordak
  • 2,060
  • 22
  • 32
  • We have changed the Manifest file(added to the question) similar to the above but still encountering the same error – Vikram May 26 '15 at 12:20