我在我的应用程序中使用Facebook SDK。我的要求是为不同的用户维护Facebook会话。我在sqlite数据库中拥有所有用户名。我使用以下代码检查用户是否已登录Facebook。如果是第一次,我显示Facebook对话框并获取访问令牌,并存储在db中以供特定用户使用,并使该用户的会话无效。当第二个用户尝试登录Facebook时,问题就出现了。出现登录对话框时,它会在log cat中显示泄露的窗口错误。
db = Stats.this.openOrCreateDatabase(DB_NAME,
MODE_PRIVATE, null);
Cursor cur = db.rawQuery("SELECT * FROM " + USER_TABLE_NAME, null);
if (cur != null) {
if (cur.moveToFirst()) {
do {
String reader_name = cur.getString(cur
.getColumnIndex("username"));
Log.v("reader_name", reader_name);
if (reader_name.equals(reader_name_db)) {
if (cur.getString(cur.getColumnIndex("fb_token"))
.equals("0")) {
authenticatedFacebook.authorize(Stats.this,
PERMISSIONS, -1,
new TestLoginListener());
} else {
accesstoken = cur.getString(cur
.getColumnIndex("fb_token"));
Log.v("AccessToken", accesstoken);
}
}
} while (cur.moveToNext());
}
}
cur.close();
db.close();
if (!accesstoken.equals("0")) {
Toast.makeText(Stats.this, "Access token already present",
Toast.LENGTH_LONG).show();
shareThroughFB();
}
public class TestLoginListener implements DialogListener {
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
if (authenticatedFacebook.isSessionValid()) {
accesstoken = authenticatedFacebook.getAccessToken();
Log.v("AccessToken", accesstoken);
db = Stats.this.openOrCreateDatabase(REDOMETER_DB_NAME,
MODE_PRIVATE, null);
db.execSQL("update " + USER_TABLE_NAME + " set fb_token='"
+ accesstoken + "' where username='" + reader_name_db
+ "'");
db.close();
FbDialog.mSpinner.dismiss();
fb_Logout();
shareThroughFB();
}
}
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
public void onCancel() {
// TODO Auto-generated method stub
}
}
它显示以下错误。
10-29 08:41:55.500: E/WindowManager(16738): Activity com.readometer.ReadometerTab has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42b4e270 V.E..... R.....I. 0,0-1080,1920} that was originally added here
10-29 08:41:55.500: E/WindowManager(16738): android.view.WindowLeaked: Activity com.readometer.ReadometerTab has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42b4e270 V.E..... R.....I. 0,0-1080,1920} that was originally added here
10-29 08:41:55.500: E/WindowManager(16738): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:426)
10-29 08:41:55.500: E/WindowManager(16738): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:235)
10-29 08:41:55.500: E/WindowManager(16738): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-29 08:41:55.500: E/WindowManager(16738): at android.app.Dialog.show(Dialog.java:282)
10-29 08:41:55.500: E/WindowManager(16738): at com.facebook.android.Facebook.dialog(Facebook.java:628)
10-29 08:41:55.500: E/WindowManager(16738): at com.facebook.android.Facebook.startDialogAuth(Facebook.java:297)
10-29 08:41:55.500: E/WindowManager(16738): at com.facebook.android.Facebook.authorize(Facebook.java:195)
10-29 08:41:55.500: E/WindowManager(16738): at com.readometer.Stats.onClick(Stats.java:596)
10-29 08:41:55.500: E/WindowManager(16738): at android.view.View.performClick(View.java:4354)
10-29 08:41:55.500: E/WindowManager(16738): at android.view.View$PerformClick.run(View.java:17961)
10-29 08:41:55.500: E/WindowManager(16738): at android.os.Handler.handleCallback(Handler.java:725)
10-29 08:41:55.500: E/WindowManager(16738): at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 08:41:55.500: E/WindowManager(16738): at android.os.Looper.loop(Looper.java:137)
10-29 08:41:55.500: E/WindowManager(16738): at android.app.ActivityThread.main(ActivityThread.java:5328)
10-29 08:41:55.500: E/WindowManager(16738): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 08:41:55.500: E/WindowManager(16738): at java.lang.reflect.Method.invoke(Method.java:511)
10-29 08:41:55.500: E/WindowManager(16738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-29 08:41:55.500: E/WindowManager(16738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-29 08:41:55.500: E/WindowManager(16738): at dalvik.system.NativeStart.main(Native Method)
10-29 08:41:55.510: E/WindowManager(16738): Activity com.readometer.ReadometerTab has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42b592d8 V.E..... R.....ID 0,0-502,346} that was originally added here
10-29 08:41:55.510: E/WindowManager(16738): android.view.WindowLeaked: Activity com.readometer.ReadometerTab has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42b592d8 V.E..... R.....ID 0,0-502,346} that was originally added here
10-29 08:41:55.510: E/WindowManager(16738): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:426)
10-29 08:41:55.510: E/WindowManager(16738): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:235)
10-29 08:41:55.510: E/WindowManager(16738): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-29 08:41:55.510: E/WindowManager(16738): at android.app.Dialog.show(Dialog.java:282)
10-29 08:41:55.510: E/WindowManager(16738): at com.facebook.android.FbDialog$FbWebViewClient.onPageStarted(FbDialog.java:197)
10-29 08:41:55.510: E/WindowManager(16738): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:399)
10-29 08:41:55.510: E/WindowManager(16738): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 08:41:55.510: E/WindowManager(16738): at android.os.Looper.loop(Looper.java:137)
10-29 08:41:55.510: E/WindowManager(16738): at android.app.ActivityThread.main(ActivityThread.java:5328)
10-29 08:41:55.510: E/WindowManager(16738): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 08:41:55.510: E/WindowManager(16738): at java.lang.reflect.Method.invoke(Method.java:511)
10-29 08:41:55.510: E/WindowManager(16738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-29 08:41:55.510: E/WindowManager(16738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-29 08:41:55.510: E/WindowManager(16738): at dalvik.system.NativeStart.main(Native Method)
10-29 08:41:56.090: I/GATE(16738): <GATE-M>DEV_ACTION_COMPLETED</GATE-M>
10-29 08:41:56.090: D/AndroidRuntime(16738): Shutting down VM
10-29 08:41:56.095: W/dalvikvm(16738): threadid=1: thread exiting with uncaught exception (group=0x413a1930)
10-29 08:41:56.100: E/AndroidRuntime(16738): FATAL EXCEPTION: main
10-29 08:41:56.100: E/AndroidRuntime(16738): java.lang.IllegalArgumentException: View not attached to window manager
10-29 08:41:56.100: E/AndroidRuntime(16738): at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:402)
10-29 08:41:56.100: E/AndroidRuntime(16738): at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:304)
10-29 08:41:56.100: E/AndroidRuntime(16738): at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
10-29 08:41:56.100: E/AndroidRuntime(16738): at android.app.Dialog.dismissDialog(Dialog.java:325)
10-29 08:41:56.100: E/AndroidRuntime(16738): at android.app.Dialog.dismiss(Dialog.java:307)
10-29 08:41:56.100: E/AndroidRuntime(16738): at com.facebook.android.FbDialog$FbWebViewClient.onPageFinished(FbDialog.java:203)
10-29 08:41:56.100: E/AndroidRuntime(16738): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:408)
10-29 08:41:56.100: E/AndroidRuntime(16738): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 08:41:56.100: E/AndroidRuntime(16738): at android.os.Looper.loop(Looper.java:137)
10-29 08:41:56.100: E/AndroidRuntime(16738): at android.app.ActivityThread.main(ActivityThread.java:5328)
10-29 08:41:56.100: E/AndroidRuntime(16738): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 08:41:56.100: E/AndroidRuntime(16738): at java.lang.reflect.Method.invoke(Method.java:511)
10-29 08:41:56.100: E/AndroidRuntime(16738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-29 08:41:56.100: E/AndroidRuntime(16738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-29 08:41:56.100: E/AndroidRuntime(16738): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
删除此行
FbDialog.mSpinner.dismiss();
如果对话框没有消失,请将其放入try / catch:
try{
FbDialog.mSpinner.dismiss();
} catch(Exception e )
{ e.printStackTrace(); }
发布你的stacktrace;)