我试图在Android上创建Facebook登录,但在运行应用时出现以下致命异常:
java.lang.RuntimeException: Unable to start activity ComponentInfo{at.friendlyride.friendlyride/at.friendlyride.friendlyride.LaunchActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class com.facebook.login.widget.LoginButton
我已按照Facebook Dev页面上的说明使用了XML代码,但我无法找到错误。这是我的代码:
// I have many more Imports, but I think this is the important one:
import com.facebook.login.widget.LoginButton;
//---------
public class LaunchFragment extends Fragment {
private TextView textDetails;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
private CallbackManager callbackManager;
private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
if (profile != null){
textDetails.setText("Welcome "+profile.getName());
}
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
};
public LaunchFragment(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
}
};
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_launch, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_friends"));
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, mCallback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onStop() {
super.onStop();
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
@Override
public void onDestroy() {
super.onDestroy();
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
}
带有Facebook登录按钮的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
tools:context=".LaunchActivity"
android:background="#953395">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:textColor="#ffffff"
android:layout_centerHorizontal="true"/>
<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_centerInParent="true"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
</RelativeLayout>
如果有人可以帮助我,那将是很棒的,我需要这个用于我最后的学校项目:)
答案 0 :(得分:0)
您是否在AndroidManifest.xml中记录了facebook活动和元数据?
答案 1 :(得分:0)
我认为这是工作
使用FacebookSdk.sdkInitialize(getApplicationContext());
之前
setContentView(R.layout.your_activity_layout);