我使用com.facebook.widget.UserSettingsFragment
在我的应用中管理登录/退出Facebook:
UserSettingsFragment userSettingsFragment = new UserSettingsFragment();
Session session = Session.getActiveSession();
if(session==null || session.isClosed()){
userSettingsFragment.setPublishPermissions(Arrays.asList("publish_actions"));
}
fragmentTransaction.replace(R.id.content_container, userSettingsFragment, "userSettingsFragment");
它似乎正确登录/注销,但是当我已经记录并暂停时(例如,通过替换Fragment
)启动此Fragment
时,它会崩溃。似乎它有一些线程正在运行,当它完成时,它需要Activity
并且它不再存在,所以它崩溃了。
对我来说这看起来像个错误,或者我错过了什么?
08-18 12:16:58.345: E/AndroidRuntime(18068): java.lang.IllegalStateException: Fragment UserSettingsFragment{420c3a08} not attached to Activity
08-18 12:16:58.345: E/AndroidRuntime(18068): at android.support.v4.app.Fragment.getResources(Fragment.java:579)
08-18 12:16:58.345: E/AndroidRuntime(18068): at com.facebook.widget.UserSettingsFragment.processImageResponse(UserSettingsFragment.java:383)
08-18 12:16:58.345: E/AndroidRuntime(18068): at com.facebook.widget.UserSettingsFragment.access$7(UserSettingsFragment.java:379)
08-18 12:16:58.345: E/AndroidRuntime(18068): at com.facebook.widget.UserSettingsFragment$2.onCompleted(UserSettingsFragment.java:370)
08-18 12:16:58.345: E/AndroidRuntime(18068): at com.facebook.widget.ImageDownloader$1.run(ImageDownloader.java:161)
08-18 12:16:58.345: E/AndroidRuntime(18068): at android.os.Handler.handleCallback(Handler.java:730)
08-18 12:16:58.345: E/AndroidRuntime(18068): at android.os.Handler.dispatchMessage(Handler.java:92)
08-18 12:16:58.345: E/AndroidRuntime(18068): at android.os.Looper.loop(Looper.java:137)
08-18 12:16:58.345: E/AndroidRuntime(18068): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-18 12:16:58.345: E/AndroidRuntime(18068): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 12:16:58.345: E/AndroidRuntime(18068): at java.lang.reflect.Method.invoke(Method.java:525)
08-18 12:16:58.345: E/AndroidRuntime(18068): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-18 12:16:58.345: E/AndroidRuntime(18068): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-18 12:16:58.345: E/AndroidRuntime(18068): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
答案 1 :(得分:2)
我见过类似的行为。如果我在登录后关闭UserSettingsFragment,但在图像更新之前,我会收到无活动连接崩溃。
我正在使用以下解决方法。在UserSettingsFragment.java中,我修改了processImageResponse(String id,ImageResponse response)以检查是否添加了片段:
private void processImageResponse(String id, ImageResponse response) {
if (response != null && UserSettingsFragment.this.isAdded()) {
Bitmap bitmap = response.getBitmap();
if (bitmap != null) {
BitmapDrawable drawable = new BitmapDrawable(UserSettingsFragment.this.getResources(), bitmap);
drawable.setBounds(0, 0,
getResources().getDimensionPixelSize(R.dimen.com_facebook_usersettingsfragment_profile_picture_width),
getResources().getDimensionPixelSize(R.dimen.com_facebook_usersettingsfragment_profile_picture_height));
userProfilePic = drawable;
userProfilePicID = id;
connectedStateLabel.setCompoundDrawables(null, drawable, null, null);
connectedStateLabel.setTag(response.getRequest().getImageUri());
}
}
}
直接更新SDK显然不太理想,但两者都不会让我的应用程序崩溃: - )