16

我使用 3.0 文档中的步骤构建了 Facebook 集成。当我使用 Eclispe 将应用程序部署到手机时,它运行良好;但是,当我导出应用程序以将其上传到商店并侧面加载 apk 时,它会失败并出现以下错误。当我导出 SDK 时,SDK 就在那里并已加载。关于我哪里出错的任何线索?

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

这是清单:

<?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>

带有登录按钮的启动页面:

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);
        }
    };

}

编辑: - - - - - - - - - - - - - - - - - - - - - - - -

这是我尝试过的:

  1. 尝试清理我的项目和 FacebookSDK 大约 50 次。
  2. 删除了 FacebookSDK 并阅读了 3 次。

我已经尝试了在互联网上可以找到的所有内容,但无法弄清楚问题所在。

4

14 回答 14

55

FacebookSdk.sdkInitialize(getApplicationContext());

这个在 setContentView 之前

  setContentView(R.layout.activity_home);
于 2015-07-23T10:10:50.197 回答
11

如果您使用的是 Facebook SDK 4.x,请注意包名称已更改。下雪了com.facebook.login.widget.LoginButton

于 2015-06-11T15:46:58.233 回答
6

我收到此错误是因为我在 sdkInitialize 之前设置了内容视图。入门指南对此有评论。

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.
}

所以我的代码现在看起来像这样:

@Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     FacebookSdk.sdkInitialize(getApplicationContext());
     setContentView(R.layout.activity_main);
}
于 2015-08-16T09:56:34.790 回答
4

我遇到了同样的错误,我刚刚清理了我的项目(facebook sdk 和当前项目)并解决了。

于 2013-09-16T14:13:01.973 回答
4

您必须在 setContentView 之前初始化 facebook SDK,因此在 setContentView 正上方添加以下行

FacebookSdk.sdkInitialize(getApplicationContext());
于 2016-05-13T08:36:26.597 回答
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.

于 2014-09-30T14:01:15.897 回答
1

我发现解决此问题的唯一方法是将整个 SDK 放入我的项目中。现在它起作用了。不是首选的解决方案,但它有效。

于 2013-03-19T15:30:12.223 回答
1

即使我尝试在模拟器中运行它,我也遇到了同样的错误。

我的问题与我将 Facebook Sdk 添加到构建路径而不是在“属性”选项卡中添加项目更相关。我从另一个 stackoverflow 答案中得到了答案

于 2013-09-04T23:07:03.030 回答
1

只需初始化 FacebookSdk.sdkInitialize(getApplicationContext()); 在您的应用程序类中。因为 Application 类甚至在你的活动被初始化之前就已经初始化了。

这样做:

公共类 ApplicationController 扩展应用程序{

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

}

于 2015-06-20T08:51:55.740 回答
0

一种解决方案是将 Facebook SDK 的路径添加到项目中的“project.properties”。

喜欢:

android.library.reference.1=../libfacebook/facebook-android-sdk-3.0.1/facebook
于 2013-07-05T06:58:45.737 回答
0

以防万一..如果有人仍然需要它,请仅清理 facebook 项目,然后将其作为库添加回您的项目中。这解决了我的问题

于 2014-09-04T18:40:28.517 回答
0

我也有同样的错误,只是用 android-support-v13.jar 替换 android-support-v4.jar 解决了这个问题。

于 2014-02-07T07:54:21.653 回答
0

如果您在 Properties->Android 中将 facebook 库添加到您的应用程序,您应该删除 facebooksdk 路径/bin/facebook.jar 中的 facebook.jar。在eclipse中链接时会导致错误。然后你清理你的 facebook sdk 和你的应用程序。我希望工作正常。

于 2015-11-10T14:00:16.157 回答
0

我尝试以编程方式创建LoginButton的实例,例如

LoginButton loginButton = new LoginButton(this);

并将其添加到LinearLayout

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

这是我的布局 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"/>

这当然是遵循@Lalit Kumar 的回答建议的编辑 让我知道我是否遗漏了什么。

于 2015-10-29T20:12:25.947 回答