无法让Facebook登录活动工作

时间:2014-01-12 19:09:18

标签: android facebook android-activity facebook-login facebook-sdk-3.0

我正在开发一款使用Facebook SDK(v3.6)的小应用程序。经过很多努力,我能够创建一个名为FBLoginActivity的活动(使用父MainActivity),该活动利用我从Facebook SDK中的样本借来的代码(即Scrumptious)。我在主活动中有一个导致FBLoginActivity的按钮(它基本上是一个类似于Scrumptious的登录页面,并且有一个LoginButton来验证用户和会话控制)。

然而,当我尝试运行应用程序并单击按钮时,应用程序崩溃了。我已将Facebook SDK导入我的工作区,所有代码构建都很好。在Facebook开发者页面上,我在我的应用程序中添加了一个Android平台,并标记了以下字段:

包名:com.example.myfbapp 类名:我不确定这是否应该是:com.example.insultdroid.MainActivity或com.example.insultdroid.FBLoginActivity;我尝试过使用这两个选项。

我还在清单中添加了必要的权限和应用ID。

我已成功生成了keyhash,并将“Single Sign On”选项设置为Yes,如开发人员教程中所示。我哪里错了?我错过了什么吗?为什么我的FBLoginActivity无法加载?

以下是FBLoginActivity的代码:

package com.example.myfbapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;

import com.facebook.AppEventsLogger;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;


public class FBLoginActivity extends FragmentActivity {

    private static final String USER_SKIPPED_LOGIN_KEY = "user_skipped_login";

    private static final int SPLASH = 0;
    private static final int SELECTION = 1;
    private static final int SETTINGS = 2;
    private static final int FRAGMENT_COUNT = SETTINGS +1;

    private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
    private MenuItem settings;
    private boolean isResumed = false;
    private boolean userSkippedLogin = false;
    private UiLifecycleHelper uiHelper;
    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);

        if (savedInstanceState != null) {
            userSkippedLogin = savedInstanceState.getBoolean(USER_SKIPPED_LOGIN_KEY);
        }
        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);

        setContentView(R.layout.activity_fblogin);

        FragmentManager fm = getSupportFragmentManager();
        SplashFragment splashFragment = (SplashFragment) fm.findFragmentById(R.id.splashFragment);
        fragments[SPLASH] = splashFragment;
        fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);
        //fragments[SETTINGS] = fm.findFragmentById(R.id.userSettingsFragment);

        FragmentTransaction transaction = fm.beginTransaction();
        for(int i = 0; i < fragments.length; i++) {
            transaction.hide(fragments[i]);
        }
        transaction.commit();

        splashFragment.setSkipLoginCallback(new SplashFragment.SkipLoginCallback() {
            @Override
            public void onSkipLoginPressed() {
                userSkippedLogin = true;
                showFragment(SELECTION, false);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        uiHelper.onResume();
        isResumed = true;

        // Call the 'activateApp' method to log an app event for use in analytics and advertising reporting.  Do so in
        // the onResume methods of the primary Activities that an app may be launched into.
        AppEventsLogger.activateApp(this);
    }

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

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }

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

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

        outState.putBoolean(USER_SKIPPED_LOGIN_KEY, userSkippedLogin);
    }

    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        Session session = Session.getActiveSession();

        if (session != null && session.isOpened()) {
            // if the session is already open, try to show the selection fragment
            showFragment(SELECTION, false);
            userSkippedLogin = false;
        } else if (userSkippedLogin) {
            showFragment(SELECTION, false);
        } else {
            // otherwise present the splash screen and ask the user to login, unless the user explicitly skipped.
            showFragment(SPLASH, false);
        }
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // only add the menu when the selection fragment is showing
        if (fragments[SELECTION].isVisible()) {
            if (menu.size() == 0) {
                settings = menu.add(R.string.settings);
            }
            return true;
        } else {
            menu.clear();
            settings = null;
        }
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.equals(settings)) {
            showSettingsFragment();
            return true;
        }
        return false;
    }

    public void showSettingsFragment() {
        showFragment(SETTINGS, true);
    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (isResumed) {
            FragmentManager manager = getSupportFragmentManager();
            int backStackSize = manager.getBackStackEntryCount();
            for (int i = 0; i < backStackSize; i++) {
                manager.popBackStack();
            }
            // check for the OPENED state instead of session.isOpened() since for the
            // OPENED_TOKEN_UPDATED state, the selection fragment should already be showing.
            if (state.equals(SessionState.OPENED)) {
                showFragment(SELECTION, false);
            } else if (state.isClosed()) {
                showFragment(SPLASH, false);
            }
        }
    }

    private void showFragment(int fragmentIndex, boolean addToBackStack) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        for (int i = 0; i < fragments.length; i++) {
            if (i == fragmentIndex) {
                transaction.show(fragments[i]);
            } else {
                transaction.hide(fragments[i]);
            }
        }
        if (addToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }
}

这是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfbapp"
    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.myfbapp.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.myfbapp.FavLogActivity"
            android:label="@string/title_activity_fav_log" >
        </activity>
        <activity
            android:name="com.example.myfbapp.FBLoginActivity"
            android:label="@string/title_activity_fblogin"
            android:parentActivityName="com.example.myfbapp.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfbapp.MainActivity" />
        </activity>
        <meta-data android:name="com.example.myfbapp.ApplicationId" android:value="@string/app_id"/>
    </application>

</manifest>

请帮帮我! 这是错误Logcat:

01-12 17:34:35.478: D/dalvikvm(7010): GC_FOR_ALLOC freed 68K, 2% free 9090K/9196K, paused 23ms, total 23ms
01-12 17:34:35.518: I/dalvikvm-heap(7010): Grow heap (frag case) to 24.726MB for 16588816-byte allocation
01-12 17:34:35.538: D/dalvikvm(7010): GC_FOR_ALLOC freed 1K, 1% free 25289K/25400K, paused 18ms, total 18ms
01-12 17:34:35.808: I/TextToSpeech(7010): Sucessfully bound to com.google.android.tts
01-12 17:34:35.848: I/Adreno-EGL(7010): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
01-12 17:34:35.878: D/OpenGLRenderer(7010): Enabling debug mode 0
01-12 17:34:38.381: D/AndroidRuntime(7010): Shutting down VM
01-12 17:34:38.381: W/dalvikvm(7010): threadid=1: thread exiting with uncaught exception (group=0x415ccba8)
01-12 17:34:38.381: E/AndroidRuntime(7010): FATAL EXCEPTION: main
01-12 17:34:38.381: E/AndroidRuntime(7010): Process: com.example.myfbapp, PID: 7010
01-12 17:34:38.381: E/AndroidRuntime(7010): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfbapp/com.example.myfbapp.FBLoginActivity}: java.lang.NullPointerException: Argument 'applicationId' cannot be null
01-12 17:34:38.381: E/AndroidRuntime(7010):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at android.os.Looper.loop(Looper.java:136)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at android.app.ActivityThread.main(ActivityThread.java:5017)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at java.lang.reflect.Method.invokeNative(Native Method)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at java.lang.reflect.Method.invoke(Method.java:515)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at dalvik.system.NativeStart.main(Native Method)
01-12 17:34:38.381: E/AndroidRuntime(7010): Caused by: java.lang.NullPointerException: Argument 'applicationId' cannot be null
01-12 17:34:38.381: E/AndroidRuntime(7010):     at com.facebook.internal.Validate.notNull(Validate.java:29)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at com.facebook.Session.<init>(Session.java:227)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at com.facebook.Session.<init>(Session.java:212)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at com.facebook.UiLifecycleHelper.onCreate(UiLifecycleHelper.java:87)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at com.example.myfbapp.FBLoginActivity.onCreate(FBLoginActivity.java:48)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at android.app.Activity.performCreate(Activity.java:5231)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-12 17:34:38.381: E/AndroidRuntime(7010):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-12 17:34:38.381: E/AndroidRuntime(7010):     ... 11 more
01-12 17:34:40.713: I/Process(7010): Sending signal. PID: 7010 SIG: 9

希望这有帮助!

我不明白Scrumptious清单中的这段代码。我的清单中没有此代码。它是什么,是否需要?

<provider android:authorities="com.facebook.app.NativeAppCallContentProvider233936543368280"
                  android:name="com.facebook.NativeAppCallContentProvider" />

“233936543368280”是Scrumptious的应用程序。

1 个答案:

答案 0 :(得分:1)

使用以下命令更改app id清单:

        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>