无法为Android初始化LoginButton Facebook-sdk

即时通讯工作在一个Android项目上,我想把一个Facebookloginfunction,我正确地添加了Facebook SDK,但我无法初始化loginbutton

**java.lang.NoClassDefFoundError: Could not initialize class com.facebook.login.widget.LoginButton** at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:413) at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:105) at com.android.tools.idea.rendering.LayoutlibCallback.loadView(LayoutlibCallback.java:176) at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207) at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:132) at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:806) at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64) at android.view.LayoutInflater.rInflate(LayoutInflater.java:782) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:385) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:400) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:332) at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350) at com.android.tools.idea.rendering.RenderService$5.compute(RenderService.java:708) at com.android.tools.idea.rendering.RenderService$5.compute(RenderService.java:697) at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:932) at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:697) at com.android.tools.idea.rendering.RenderService.render(RenderService.java:816) at com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel$6.run(AndroidDesignerEditorPanel.java:480) at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320) at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310) at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254) at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269) at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227) at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217) at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238) at com.intellij.util.Alarm$Request$1.run(Alarm.java:327) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) 

这里是我的AppManifest.xml

 <?xml version="1.0" encoding="utf-8"?> 

 <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".helloActivity" android:label="@string/title_activity_hello" > </activity> <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" /> <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/> </application> 

我在Strings.xml中指定了App_id。 感谢帮助

Solutions Collecting From Web of "无法为Android初始化LoginButton Facebook-sdk"

这是由于FacebookSdk在使用前未初始化。 只需添加下面的行:

FacebookSdk.sdkInitialize(mContext);

如果主机的LoginButton是:
1.Activity-在setContentView(R.layout.activity_main)之前setContentView(R.layout.activity_main)
2.Fragment-在inflater.inflate之前inflater.inflate(R.layout.fragment_main, container, false)

在上面的例子中,不要忘记在onActivityResult()中监听callback,以更新LoginButton的UI。

我检查了最新的SDK4.0.1,在com.facebook.login.widget.LoginButton可用。 但是在facebook-android-sdk-3.17.1中没有find,那里只有com.facebook.widget.LoginButton可用。 Facebook没有通知开发者或他们的更新日志LoginButton类移动到子文件夹。

很奇怪,为什么Facebook没有正确更新..即使是Androidlogin文档是错误的!

添加这个给你Manifest.xml

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