我正在尝试创建一个应用程序,允许用户通过他们的Facebook帐户登录。我想通过eclipse使用新的Facebook SDK 4.1.2。要在eclipse中使用新的SDK,我遵循了this tutorial。我现在能够在没有eclipse的情况下从facebook SDK中删除所有错误。然后,我开始关注these steps,以便使用新SDK将FB集成到示例应用程序中。
我继续说:
MainActivity :
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.facebook.CallbackManager;
import com.facebook.FacebookSdk;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FacebookSdk.sdkInitialize(getApplicationContext());
if (savedInstanceState == null) {
getFragmentManager()
.beginTransaction()
.add(R.id.fragmentParentViewGroup, new FacebookFragment())
.commit();
}
}
}
activity_main.xml中:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragmentParentViewGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity"
tools:ignore="MergeRootFrame" />
FacebookFragment:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
public class FacebookFragment extends Fragment{
CallbackManager callbackManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parentViewGroup,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.facebook_fragment, parentViewGroup, false);
FacebookSdk.sdkInitialize(getActivity());
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) rootView.findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
// If using in a fragment
// loginButton.setFragment(this);
// Callback registration
loginButton.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
}
});
return rootView;
}
}
facebook_fragment.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<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 xmlns:android="http://schemas.android.com/apk/res/android"
package="com.facbooksdk4test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<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" >
<activity
android:name="com.facbooksdk4test.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<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" />
</application>
</manifest>
完成上述所有操作后,我仍然遇到初始化错误。 logcat报告:
06-01 15:23:41.670: E/AndroidRuntime(23571): FATAL EXCEPTION: main
06-01 15:23:41.670: E/AndroidRuntime(23571): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.facbooksdk4test/com.facbooksdk4test.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class com.facebook.login.widget.LoginButton
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.app.ActivityThread.access$700(ActivityThread.java:153)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.os.Looper.loop(Looper.java:176)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.app.ActivityThread.main(ActivityThread.java:5302)
06-01 15:23:41.670: E/AndroidRuntime(23571): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 15:23:41.670: E/AndroidRuntime(23571): at java.lang.reflect.Method.invoke(Method.java:511)
06-01 15:23:41.670: E/AndroidRuntime(23571): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-01 15:23:41.670: E/AndroidRuntime(23571): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-01 15:23:41.670: E/AndroidRuntime(23571): at dalvik.system.NativeStart.main(Native Method)
06-01 15:23:41.670: E/AndroidRuntime(23571): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class com.facebook.login.widget.LoginButton
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.view.LayoutInflater.createView(LayoutInflater.java:619)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-01 15:23:41.670: E/AndroidRuntime(23571): at com.facbooksdk4test.FacebookFragment.onCreateView(FacebookFragment.java:22)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.app.Fragment.performCreateView(Fragment.java:1699)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.app.BackStackRecord.run(BackStackRecord.java:682)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.app.Activity.performStart(Activity.java:5335)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
06-01 15:23:41.670: E/AndroidRuntime(23571): ... 11 more
06-01 15:23:41.670: E/AndroidRuntime(23571): Caused by: java.lang.reflect.InvocationTargetException
06-01 15:23:41.670: E/AndroidRuntime(23571): at java.lang.reflect.Constructor.constructNative(Native Method)
06-01 15:23:41.670: E/AndroidRuntime(23571): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
06-01 15:23:41.670: E/AndroidRuntime(23571): at android.view.LayoutInflater.createView(LayoutInflater.java:593)
06-01 15:23:41.670: E/AndroidRuntime(23571): ... 24 more
06-01 15:23:41.670: E/AndroidRuntime(23571): Caused by: java.lang.NoClassDefFoundError: com.facebook.R$styleable
06-01 15:23:41.670: E/AndroidRuntime(23571): at com.facebook.login.widget.LoginButton.parseLoginButtonAttributes(LoginButton.java:607)
06-01 15:23:41.670: E/AndroidRuntime(23571): at com.facebook.login.widget.LoginButton.configureButton(LoginButton.java:571)
06-01 15:23:41.670: E/AndroidRuntime(23571): at com.facebook.FacebookButtonBase.<init>(FacebookButtonBase.java:66)
06-01 15:23:41.670: E/AndroidRuntime(23571): at com.facebook.login.widget.LoginButton.<init>(LoginButton.java:209)
06-01 15:23:41.670: E/AndroidRuntime(23571): ... 27 more
我不确定我哪里出错,或者应该采取什么措施来解决问题。请告诉我如何解决这个问题。
答案 0 :(得分:5)
我没有在您的代码onActivityResult
方法中看到。适当的SDK工作流程是必需的。
您需要将其添加到MainActivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
原因:
您与FacebookSDK集成的每个活动和片段 登录或共享应将onActivityResult转发给callbackManager。
您可以在文档here中找到它。
答案 1 :(得分:3)
由于FacebookSdk在使用之前未初始化,可能会出现此问题。 尝试在之前初始化facebook sdk:
1.setContentView(R.layout.activity_main); (在活动中)
2.View rootView = inflater.inflate(R.layout.facebook_fragment,parentViewGroup,false); (在片段中)
希望这有帮助!
答案 2 :(得分:1)
确保在inflater之前初始化了facebook SDK。
FacebookSdk.sdkInitialize(getActivity());
View rootView = inflater.inflate(R.layout.facebook_fragment, parentViewGroup, false);
答案 3 :(得分:0)
你在MainActivity和FacebookFragment中初始化了facebook sdk,这是不需要的,只有一次就够了,你最好在超类中初始化它,这可能在你的MainActivity中。
从FacebookFragemet
中删除此行FacebookSdk.sdkInitialize(getActivity());
并在MainActivity中交换行如下
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);