2

我使用“Parse”为 android 的 facebook 集成制作了一个简单的程序,我使用了以下代码,但显示异常,我的代码和 logcat 如下所示:

main.java

包 com.example.facbk;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;
import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;
import android.net.ParseException;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private static String APP_ID =" 454641867918042";
    private Facebook facebook;
    private AsyncFacebookRunner mAsyncRunner;
    String FILENAME = "AndroidSSO_data";
    Button login;
    private SharedPreferences mPrefs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Parse.initialize(this, "454641867918042", "aWEd6rRA18hQJNVueCzeVUqqPkkDKAxp8moP3IP");// Try by Adding this method before ParseFacebookUtils.initialize()
        ParseFacebookUtils.initialize("454641867918042");
        setContentView(R.layout.activity_main);
        System.out.println("hello login");
        ParseFacebookUtils.logIn(this, new LogInCallback() {

            @Override
            public void done(ParseUser user, com.parse.ParseException arg1) {
                // TODO Auto-generated method stub
                 if (user == null) {
                      Log.d("MyApp", "Uh oh. The user cancelled the Facebook login.");
                    } else if (user.isNew()) {
                      Log.d("MyApp", "User signed up and logged in through Facebook!");
                    } else {
                      Log.d("MyApp", "User logged in through Facebook!");
                    }
            }
            });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      ParseFacebookUtils.finishAuthentication(requestCode, resultCode, data);
    }

}

日志猫

07-11 11:04:34.751: I/dalvikvm(8972): threadid=3: reacting to signal 3
07-11 11:04:34.951: I/dalvikvm(8972): Wrote stack traces to '/data/anr/traces.txt'
07-11 11:04:35.190: I/dalvikvm(8972): threadid=3: reacting to signal 3
07-11 11:04:35.331: I/dalvikvm(8972): Wrote stack traces to '/data/anr/traces.txt'
07-11 11:04:35.631: I/System.out(8972): hello login
07-11 11:04:35.701: I/dalvikvm(8972): threadid=3: reacting to signal 3
07-11 11:04:35.801: D/AndroidRuntime(8972): Shutting down VM
07-11 11:04:35.801: I/dalvikvm(8972): Wrote stack traces to '/data/anr/traces.txt'
07-11 11:04:35.831: W/dalvikvm(8972): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
07-11 11:04:35.850: E/AndroidRuntime(8972): FATAL EXCEPTION: main
07-11 11:04:35.850: E/AndroidRuntime(8972): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.facbk/com.example.facbk.MainActivity}: com.facebook.FacebookException: Cannot use SessionLoginBehavior SSO_WITH_FALLBACK when com.facebook.LoginActivity is not declared as an activity in AndroidManifest.xml
07-11 11:04:35.850: E/AndroidRuntime(8972):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at android.os.Looper.loop(Looper.java:137)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at java.lang.reflect.Method.invokeNative(Native Method)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at java.lang.reflect.Method.invoke(Method.java:511)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at dalvik.system.NativeStart.main(Native Method)
07-11 11:04:35.850: E/AndroidRuntime(8972): Caused by: com.facebook.FacebookException: Cannot use SessionLoginBehavior SSO_WITH_FALLBACK when com.facebook.LoginActivity is not declared as an activity in AndroidManifest.xml
07-11 11:04:35.850: E/AndroidRuntime(8972):     at com.facebook.Session.validateLoginBehavior(Session.java:992)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at com.facebook.Session.open(Session.java:915)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at com.facebook.Session.openForRead(Session.java:385)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at com.parse.auth.FacebookAuthenticationProvider.authenticate(FacebookAuthenticationProvider.java:156)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at com.parse.ParseUser.authenticateAsync(ParseUser.java:1091)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at com.parse.ParseUser.logInWithAsync(ParseUser.java:1108)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at com.parse.ParseUser.logInWithAsync(ParseUser.java:933)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at com.parse.ParseFacebookUtils.logIn(ParseFacebookUtils.java:338)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at com.parse.ParseFacebookUtils.logIn(ParseFacebookUtils.java:363)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at com.example.facbk.MainActivity.onCreate(MainActivity.java:35)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at android.app.Activity.performCreate(Activity.java:4465)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-11 11:04:35.850: E/AndroidRuntime(8972):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-11 11:04:35.850: E/AndroidRuntime(8972):     ... 11 more
07-11 11:04:36.190: I/dalvikvm(8972): threadid=3: reacting to signal 3
07-11 11:04:36.230: I/dalvikvm(8972): Wrote stack traces to '/data/anr/traces.txt'
07-11 11:04:36.421: I/dalvikvm(8972): threadid=3: reacting to signal 3
07-11 11:04:36.471: I/dalvikvm(8972): Wrote stack traces to '/data/anr/traces.txt'
4

2 回答 2

1

我不知道它会起作用还是不试试:打电话Parse.initialize()

您在 onCreate 中的代码应该是

super.onCreate(savedInstanceState);
Parse.initialize(this, Constants.APPLICATION_ID, Constants.CLIENT_KEY);// Try by Adding this method before ParseFacebookUtils.initialize()
ParseFacebookUtils.initialize("454641867918042");
setContentView(R.layout.activity_main);

编辑:

还要检查 AndroidManifest.xml 中的 Internet 权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

编辑:

将以下添加到您的AndroidManifest.xml

<activity
    android:name="com.facebook.LoginActivity"
     >
</activity>
于 2013-07-11T04:45:18.707 回答
0

显然,您已经为非fb 请求调用了 ParseFacebookUtils.finishAuthentication() 。阅读:https ://www.parse.com/questions/nullpointerexceptions-from-the-facebook-authentication

另一个原因可能是您使用的 fb sdk 3.0.2 已更改此部分并且 parse 不知道如何使用它。我在上面的链接中对此进行了评论。

尝试使用这个:

/* This constant is private in new fb sdk 3.0.2, this is why we use it this way. */
private final static int DEFAULT_FACEBOOK_AUTH_ACTIVITY_CODE = 32665;

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == FB_HACK_CODE && data != null)
            ParseFacebookUtils.finishAuthentication(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
}
于 2013-08-15T11:52:13.960 回答