无法使用Facebook LoginButton登录

时间:2014-01-13 19:03:03

标签: android facebook facebook-login

按照官方tutorial中给出的步骤,我尝试进行登录活动并将其集成到我的应用中。但是,每次单击LoginButton时,我的应用程序崩溃。我无法理解我哪里出错了。我已更新清单文件,输入正确的appID并生成相应的keyhash。这是代码和错误日志:

LoginFragment.java:

package com.example.insultdroid;

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 com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.LoginButton;

public class LoginFragment extends Fragment {

    private static final String TAG = "LoginFragment";
    private UiLifecycleHelper uiHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.activity_fblogin, container, false);

        LoginButton fbloginbutton = (LoginButton) view.findViewById(R.id.fbloginbutton);
        fbloginbutton.setFragment(this);

        return view;
    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            Log.i(TAG, "Logged in...");
        } else if (state.isClosed()) {
            Log.i(TAG, "Logged out...");
        }
    }

    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);
    }
    @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);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }

}

FBLoginActivity.java:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
//import android.service.textservice.SpellCheckerService.Session;


public class FBLoginActivity extends FragmentActivity {

    private LoginFragment fbloginFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) 
        {
            // Add the fragment on initial activity setup
            fbloginFragment = new LoginFragment();
            getSupportFragmentManager()
            .beginTransaction()
            .add(android.R.id.content, fbloginFragment)
            .commit();
        } 
        else 
        {
            // Or set the fragment from restored state info
            fbloginFragment = (LoginFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);
        }
    }

FBLoginActivity.xml:

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FBLoginActivity"
    android:orientation="vertical" >

    <com.facebook.widget.LoginButton
            android:id="@+id/fbloginbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="30dp" />

</LinearLayout>

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.insultdroid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.INTERNET"/>


    <application
        android:allowBackup="true"
        android:icon="@drawable/angry_android"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.insultdroid.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.insultdroid.FavLogActivity"
            android:label="@string/title_activity_fav_log" >
        </activity>
        <activity
            android:name="com.example.insultdroid.FBLoginActivity"
            android:label="@string/title_activity_fblogin"
            android:parentActivityName="com.example.insultdroid.MainActivity" >
            <meta-data


 android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.insultdroid.MainActivity" />
    </activity>
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
</application>

这是错误Logcat的输出:

01-13 14:00:34.064: D/AndroidRuntime(16332): Shutting down VM
01-13 14:00:34.064: E/AndroidRuntime(16332): FATAL EXCEPTION: main
01-13 14:00:34.064: E/AndroidRuntime(16332): Process: com.example.insultdroid, PID: 16332
01-13 14:00:34.064: E/AndroidRuntime(16332): com.facebook.FacebookException: Cannot use SessionLoginBehavior SSO_WITH_FALLBACK when com.facebook.LoginActivity is not declared as an activity in AndroidManifest.xml
01-13 14:00:34.064: E/AndroidRuntime(16332):    at com.facebook.Session.validateLoginBehavior(Session.java:1031)
01-13 14:00:34.064: E/AndroidRuntime(16332):    at com.facebook.Session.open(Session.java:953)
01-13 14:00:34.064: E/AndroidRuntime(16332):    at com.facebook.Session.openForRead(Session.java:388)
01-13 14:00:34.064: E/AndroidRuntime(16332):    at com.facebook.widget.LoginButton$LoginClickListener.onClick(LoginButton.java:713)
01-13 14:00:34.064: E/AndroidRuntime(16332):    at android.view.View.performClick(View.java:4438)
01-13 14:00:34.064: E/AndroidRuntime(16332):    at android.view.View$PerformClick.run(View.java:18422)
01-13 14:00:34.064: E/AndroidRuntime(16332):    at android.os.Handler.handleCallback(Handler.java:733)
01-13 14:00:34.064: E/AndroidRuntime(16332):    at android.os.Handler.dispatchMessage(Handler.java:95)
01-13 14:00:34.064: E/AndroidRuntime(16332):    at android.os.Looper.loop(Looper.java:136)
01-13 14:00:34.064: E/AndroidRuntime(16332):    at android.app.ActivityThread.main(ActivityThread.java:5017)
01-13 14:00:34.064: E/AndroidRuntime(16332):    at java.lang.reflect.Method.invoke(Native Method)
01-13 14:00:34.064: E/AndroidRuntime(16332):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-13 14:00:34.064: E/AndroidRuntime(16332):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

请有人帮助我!谢谢!

1 个答案:

答案 0 :(得分:1)

您的清单文件中未定义FBLoginActivity和LoginFragment