2016-01-06 44 views
0

我试图创建Android上的Facebook登录,但在运行应用程序时,我得到以下致命异常:Android Studio中的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 

我已经使用了XML代码上的指示Facebook的开发页面,我无法找到错误。这是我的代码:

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

您是否在AndroidManifest.xml中记录了Facebook活动和元数据?

+0

是的,我有,但我抬起头,另一种解决方案是现在的工作,谢谢:) –

0

我觉得这是工作

使用FacebookSdk.sdkInitialize(getApplicationContext());

setContentView(R.layout.your_activity_layout);