16

I have built a Facebook integration using the steps from the 3.0 documentation. It works fine in when I deploy the app to my phone using Eclispe; however, when I export the app to upload it to the store and side load the apk, it fails with the following error. The SDK is there and loaded, when I export it. Any clue on where I am going wrong?

03-18 21:01:02.089: E/AndroidRuntime(14834): FATAL EXCEPTION: main
03-18 21:01:02.089: E/AndroidRuntime(14834): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.telvista.facebooksupport/com.telvista.facebooksupport.SplashActivity}: android.view.InflateException: Binary XML file line #51: Error inflating class com.facebook.widget.LoginButton
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.app.ActivityThread.access$700(ActivityThread.java:139)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.os.Looper.loop(Looper.java:137)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.app.ActivityThread.main(ActivityThread.java:4918)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at java.lang.reflect.Method.invokeNative(Native Method)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at java.lang.reflect.Method.invoke(Method.java:511)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at dalvik.system.NativeStart.main(Native Method)
03-18 21:01:02.089: E/AndroidRuntime(14834): Caused by: android.view.InflateException: Binary XML file line #51: Error inflating class com.facebook.widget.LoginButton
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.app.Activity.setContentView(Activity.java:1901)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at com.telvista.facebooksupport.SplashActivity.onCreate(SplashActivity.java:29)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.app.Activity.performCreate(Activity.java:5048)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2052)
03-18 21:01:02.089: E/AndroidRuntime(14834):    ... 11 more
03-18 21:01:02.089: E/AndroidRuntime(14834): Caused by: java.lang.reflect.InvocationTargetException
03-18 21:01:02.089: E/AndroidRuntime(14834):    at java.lang.reflect.Constructor.constructNative(Native Method)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
03-18 21:01:02.089: E/AndroidRuntime(14834):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
03-18 21:01:02.089: E/AndroidRuntime(14834):    ... 23 more
03-18 21:01:02.089: E/AndroidRuntime(14834): Caused by: java.lang.NoClassDefFoundError: com.facebook.android.R$color
03-18 21:01:02.089: E/AndroidRuntime(14834):    at com.facebook.widget.LoginButton.<init>(LoginButton.java:204)
03-18 21:01:02.089: E/AndroidRuntime(14834):    ... 26 more

Here is the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.t.facebooksupport"
    android:versionCode="21"
    android:versionName="2.1" >

    <uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
    android:allowBackup="false"
    android:icon="@drawable/t_logo"
    android:label="@string/app_name">

    <activity
        android:theme="@android:style/Theme.NoTitleBar"
        android:name="com.t.facebooksupport.SplashActivity"
        android:icon="@drawable/t_logo"
        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="com.t.facebooksupport.MainActivity">
    </activity>
    <activity
        android:name="com.t.facebooksupport.ViewPost">
    </activity>
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
    <activity android:label="@string/app_name" android:name="com.facebook.LoginActivity"></activity>

    </application>

</manifest>

Splash page with login button:

package com.t.facebooksupport;

import java.util.Arrays;
import java.util.Map;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.t.facebooksupport.helper.PostConstants;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

public class SplashActivity extends Activity {

    private UiLifecycleHelper uiHelper;
    private boolean isResumed = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        uiHelper = new UiLifecycleHelper(this, callback);
        Session session = Session.getActiveSession();

        if (session != null && session.isOpened()) {
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
        }
        LoginButton authButton = (LoginButton) findViewById(R.id.login_button);
        authButton.setReadPermissions(Arrays.asList("email"));

    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (isResumed) {
            if (state.isOpened()) {
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            // Display the parsed user info
                            SharedPreferences settings = getSharedPreferences(PostConstants.PREFS_NAME, Activity.MODE_PRIVATE);
                            SharedPreferences.Editor editor = settings.edit();
                            editor.putString("fb_id", user.getId());
                            editor.putString("fullname", user.getFirstName() + " " + user.getLastName());
                            editor.putString("firstname", user.getFirstName());
                            editor.putString("lastname", user.getLastName());
                            editor.putString("image_loc", "https://graph.facebook.com/" + user.getId() + "/picture?type=large");
                            editor.putString("user_id", user.asMap().get("email").toString());
                            editor.putString("email", user.asMap().get("email").toString());
                            editor.putString("page", "1");
                            editor.putString("count", "30");
                            editor.putString("token", "");
                            editor.commit();

                            startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        }
                    }
                });
            } else if (state.isClosed()) {
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        uiHelper.onResume();
        isResumed = true;
    }

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
        isResumed = false;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }

    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

}

EDIT: -----------------------------------------------

Here is what I have tried:

  1. Tried cleaning both my project and the FacebookSDK about 50 times.
  2. Removed the FacebookSDK and readded it 3 times.

I have tried everything I can find on the internet and cannot figure out what the issue is.

Toby Jones
  • 353
  • 2
  • 5
  • 13

14 Answers14

55

FacebookSdk.sdkInitialize(getApplicationContext());

this before setContentView

  setContentView(R.layout.activity_home);
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Lalit kumar
  • 2,377
  • 1
  • 22
  • 17
11

If you are using Facebook SDK 4.x, then note that the package name changed. It's now com.facebook.login.widget.LoginButton

Sandy
  • 2,572
  • 7
  • 40
  • 61
6

I was getting this error because I set the content view BEFORE sdkInitialize. The getting started guide has a comment referring to this.

https://developers.facebook.com/docs/android/getting-started#androidstudio

@Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     FacebookSdk.sdkInitialize(getApplicationContext());
     // Initialize the SDK before executing any other operations,
     // especially, if you're using Facebook UI elements.
}

So my code now looks like this:

@Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     FacebookSdk.sdkInitialize(getApplicationContext());
     setContentView(R.layout.activity_main);
}
Travis Rotz
  • 153
  • 1
  • 9
4

I had the same error, I just cleaned my projects (facebook sdk and current project) and it was resolved.

Fabricioraphael
  • 195
  • 1
  • 9
4

you have to initialize facebook SDK before setContentView, so add following line right above the setContentView

FacebookSdk.sdkInitialize(getApplicationContext());
tharinduNA
  • 580
  • 5
  • 12
3

After add Facebook as a module you have to add it to your dependencies in Gradle, adding the next line into dependencies block:

compile project(':facebook')

I hope it can help.

lm2a
  • 835
  • 1
  • 10
  • 19
1

The only way I found to fix this was to put the whole SDK into my project. Now it works. Not the prefered solution, but it works.

Toby Jones
  • 353
  • 2
  • 5
  • 13
  • That's the only way to do it...Without the whole SDK it has no clue what color that is refering to – Darussian Mar 19 '13 at 17:29
  • How to do this? Put the facebooksdk.jar in the lib folder, but I still get the same error – José Castro Mar 22 '13 at 01:51
  • I determinate the error, it's because the route to class component it's incorrect. For get a solution I had rewrite the path to my class (then the adt get stupid and I had to restart) and done. – José Castro Mar 22 '13 at 02:43
  • @JosephCastro: could you be more specific? Where do you had to rewrite the path? Plz, this is getting me crazy! – Marco Masci Jun 23 '13 at 20:11
  • @MarcoMasci look the class name in the xml file and see if the classpath is ok – José Castro Jun 24 '13 at 22:02
1

I had the same error even when I tried to run it in the emulator.

My problem was more related that I added the Facebook Sdk to the build path instead of adding the project in the Properties tab. I got the answer from another stackoverflow answer

Community
  • 1
  • 1
e3matheus
  • 2,112
  • 1
  • 20
  • 28
1

Just initialize FacebookSdk.sdkInitialize(getApplicationContext()); in your Application class. Because Application class initializes even before your activities gets initialized.

do just like this :

public class ApplicationController extends Application{

@Override
public void onCreate() {
    super.onCreate();
    FacebookSdk.sdkInitialize(getApplicationContext());
}

}

0

One solution is to add the path of the Facebook SDK to "project.properties" in your project.

Like:

android.library.reference.1=../libfacebook/facebook-android-sdk-3.0.1/facebook
Jason Wang
  • 111
  • 1
  • 2
0

I had also same error and just by relacing android-support-v4.jar with android-support-v13.jar solved the problem.

Hamza Khalil
  • 481
  • 1
  • 7
  • 18
0

just in case.. if some one still needs it, clean only the facebook project, and then add it back as a library in your project. This solved my problem

Prajwal Udupa
  • 860
  • 5
  • 28
0

I tried Programatically creating instance of LoginButton programatically like

LoginButton loginButton = new LoginButton(this);

and added this to a LinearLayout

    fbLoginButton = (LinearLayout) findViewById(R.id.fb_login_view);
    fbLoginButton.addView(loginButton);

And here is the glimpse of my layout xml

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:id="@+id/fb_login_view"
            android:layout_centerInParent="true"
            android:layout_gravity="center_horizontal"
            android:text="connect_with_facebook"/>

This is ofcourse following the edit suggested by @Lalit Kumar's answer Let me know if I am missing anything.

Community
  • 1
  • 1
Its not blank
  • 3,055
  • 22
  • 37
0

If you add facebook library to your application at Properties->Android , you should delete facebook.jar in your facebooksdk path/bin/facebook.jar. It cause error when link in eclipse. Then you clean your facebook sdk and your application. I hope will work fine.