Error inflating class com.facebook.widget.LoginButton

Toby Jones picture Toby Jones · Mar 19, 2013 · Viewed 28.1k times · Source

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.

Answer

Lalit kumar picture Lalit kumar · Jul 23, 2015

FacebookSdk.sdkInitialize(getApplicationContext());

this before setContentView

  setContentView(R.layout.activity_home);