我正在开发第三方Facebook Android应用。到目前为止,我的代码运行良好,但是当我在下面添加Facebook登录代码时,我的应用程序崩溃了:
import java.util.Arrays;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
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.facebook.widget.ProfilePictureView;
public class MainFragment extends Fragment {
private static final String TAG = "MainFragment";
private static final int REAUTH_ACTIVITY_CODE = 100;
private ProfilePictureView profilePictureView;
private TextView userNameView;
private final List<String> permissions;
public MainFragment() {
permissions = Arrays.asList("user_status", "read_stream", "friends_about_me", "friends_work_history", "friends_activities",
"friends_birthday", "user_checkins", "friends_checkins", "user_education_history", "friends_education_history", "user_events",
"friends_events", "user_groups", "friends_groups", "user_hometown", "friends_hometown", "user_interests", "friends_interests",
"user_likes", "friends_likes", "user_notes", "friends_notes", "user_online_presence", "friends_online_presence", "user_interests",
"friends_interests", "user_likes", "friends_likes", "user_notes", "friends_notes", "user_online_presence", "friends_online_presence",
"user_religion_politics", "friends_religion_politics", "user_status", "friends_status", "user_subscriptions",
"friends_subscriptions", "user_videos", "friends_videos", "user_website", "friends_website", "user_work_history",
"friends_work_history", "read_friendlists", "read_mailbox", "read_requests", "read_stream", "xmpp_login", "email", "user_location",
"user_photos", "friends_photos");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
private void makeMeRequest(final Session session) {
// Make an API call to get user data and define a
// new callback to handle the response.
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// If the response is successful
// Check for an open session
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// Get the user's data
makeMeRequest(session);
}
if (user != null) {
// Set the id for the ProfilePictureView
// view that in turn displays the profile picture.
profilePictureView.setProfileId(user.getId());
// Set the Textview's text to the user's name.
userNameView.setText(user.getName());
}
{
if (response.getError() != null) {
// Handle errors, will do so later.
}
}
}});
request.executeAsync();
}
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(final Session session, final SessionState state, final Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main, container, false);
// Find the user's profile picture custom view
profilePictureView = (ProfilePictureView) view.findViewById(R.id.sidebar_profile_picture);
profilePictureView.setCropped(true);
// Find the user's name view
userNameView = (TextView) view.findViewById(R.id.sidebar_profile_name);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setReadPermissions(permissions);
return view;
}
@Override
public void onResume() {
super.onResume();
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REAUTH_ACTIVITY_CODE) {
uiHelper.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
uiHelper.onSaveInstanceState(bundle);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
private void onSessionStateChange(Session session, SessionState state,
Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
getView().setVisibility(View.GONE);
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}}
是否有人可以帮助我找出导致应用崩溃的原因?
这是我的main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#324D82" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="@+id/splash_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:gravity="center"
android:src="@drawable/icon" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="28sp"
android:fontFamily="sans-serif-condensed"
android:textStyle="normal"
android:text="FrontPage for Facebook" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="10sp"
android:fontFamily="sans-serif-condensed"
android:textStyle="normal"
android:text="Version 1.0" />
</LinearLayout>
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
/>
</LinearLayout>
这是我的sidebar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="@dimen/sidebar_profile_height"
android:id="@+id/sidebar_profile"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/sidebar_profile_cover"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/test_image1"
android:scaleType="centerCrop"
android:contentDescription="@string/pref_user_profile"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/sidebar_profile_selector"
android:gravity="center_vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center_vertical"
android:paddingTop="6.0dip">
<com.facebook.widget.ProfilePictureView
android:id="@+id/sidebar_profile_picture"
android:src="@drawable/test_profile_pic"
android:layout_width="42.0dip"
android:layout_height="42.0dip"
android:scaleType="centerCrop"
android:contentDescription="@string/pref_user_profile"
android:padding="2.0dip"
android:background="@drawable/profile_picture_border"
facebook:preset_size="small" />
<TextView
android:id="@+id/sidebar_profile_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="8.0dip"
android:text="@string/test_title_text"
android:textSize="@dimen/title_text"
android:textColor="@color/white"
android:gravity="center_horizontal"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>