我需要在我的应用程序中通过Parse添加Facebook授权。我按照https://parse.com/docs/android/guide#users-facebook-users上的说明操作。但是,如果我运行应用程序,然后按“返回”按钮然后再次运行它我总是会收到此错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{.AuthActivity}: java.lang.IllegalStateException: Callback already registered for <facebook>: com.parse.ParseFacebookUtils$1@28d04f8f
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.IllegalStateException: Callback already registered for <facebook>: com.parse.ParseFacebookUtils$1@28d04f8f
at com.parse.ParseAuthenticationManager.register(ParseAuthenticationManager.java:35)
at com.parse.ParseUser.registerAuthenticationCallback(ParseUser.java:1094)
at com.parse.ParseFacebookUtils$ParseUserDelegateImpl.registerAuthenticationCallback(ParseFacebookUtils.java:562)
at com.parse.ParseFacebookUtils.initialize(ParseFacebookUtils.java:109)
at com.parse.ParseFacebookUtils.initialize(ParseFacebookUtils.java:92)
at .AuthActivity.onCreate(AuthActivity.java:47)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
这就是我的活动的样子:
public class AuthActivity extends AppCompatActivity {
…
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ParseFacebookUtils.initialize(this); // crash here
setupEvents();
…
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);
}
private void setupEvents() {
findViewById(R.id.fb_login).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginViaFacebook();
}
});
}
private void loginViaFacebook() {
ParseFacebookUtils.logInWithReadPermissionsInBackground(AuthActivity.this, null, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException err) {
…
}
});
}
}
这里有什么问题?
答案 0 :(得分:1)
您应该将ParseFacebookUtils
初始化为应用类,而不是 AppCompatActivity 类。
您可以创建一个java类并将其命名为配置,在清单中设置其名称:
<application
android:name=".Configs"
...
Configs类看起来像这样:
public class Configs extends Application {
…
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ParseFacebookUtils.initialize(this);
…
}