上下文:我创建了一个LibGDX游戏(扩展AndroidApplication),切换到另一个活动(扩展FragmentActivity)以启动社交Facebook共享活动(扩展片段)。我按照这里的教程:https://developers.facebook.com/docs/android/scrumptious/publish-open-graph-story#step6c
然而,当我尝试运行我的程序时,我得到一个Activity泄露窗口错误。完整的错误日志如下:
01-07 00:20:33.160: E/WindowManager(23786): Activity com.pressx.thedevice.SocialActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41cca0c0 that was originally added here
01-07 00:20:33.160: E/WindowManager(23786): android.view.WindowLeaked: Activity com.pressx.thedevice.SocialActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41cca0c0 that was originally added here
01-07 00:20:33.160: E/WindowManager(23786): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
01-07 00:20:33.160: E/WindowManager(23786): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:307)
01-07 00:20:33.160: E/WindowManager(23786): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:239)
01-07 00:20:33.160: E/WindowManager(23786): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:152)
01-07 00:20:33.160: E/WindowManager(23786): at android.view.Window$LocalWindowManager.addView(Window.java:547)
01-07 00:20:33.160: E/WindowManager(23786): at android.app.Dialog.show(Dialog.java:282)
01-07 00:20:33.160: E/WindowManager(23786): at android.app.ProgressDialog.show(ProgressDialog.java:116)
01-07 00:20:33.160: E/WindowManager(23786): at android.app.ProgressDialog.show(ProgressDialog.java:99)
01-07 00:20:33.160: E/WindowManager(23786): at com.pressx.facebook.SelectionFragment.handleAnnounce(SelectionFragment.java:492)
01-07 00:20:33.160: E/WindowManager(23786): at com.pressx.facebook.SelectionFragment.access$2(SelectionFragment.java:480)
01-07 00:20:33.160: E/WindowManager(23786): at com.pressx.facebook.SelectionFragment$2.onClick(SelectionFragment.java:385)
01-07 00:20:33.160: E/WindowManager(23786): at android.view.View.performClick(View.java:4101)
01-07 00:20:33.160: E/WindowManager(23786): at android.view.View$PerformClick.run(View.java:17087)
01-07 00:20:33.160: E/WindowManager(23786): at android.os.Handler.handleCallback(Handler.java:615)
01-07 00:20:33.160: E/WindowManager(23786): at android.os.Handler.dispatchMessage(Handler.java:92)
01-07 00:20:33.160: E/WindowManager(23786): at android.os.Looper.loop(Looper.java:137)
01-07 00:20:33.160: E/WindowManager(23786): at android.app.ActivityThread.main(ActivityThread.java:4849)
01-07 00:20:33.160: E/WindowManager(23786): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 00:20:33.160: E/WindowManager(23786): at java.lang.reflect.Method.invoke(Method.java:511)
01-07 00:20:33.160: E/WindowManager(23786): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
01-07 00:20:33.160: E/WindowManager(23786): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
01-07 00:20:33.160: E/WindowManager(23786): at dalvik.system.NativeStart.main(Native Method)
在周围读到我可能发生了ProgressDialog问题。但是,我按照Facebook教程运行了它创建的示例应用程序,没有发生此类错误。
以下是ProgressDialog的所有位置及其上下文:
private ProgressDialog progressDialog;
在
public class SelectionFragment extends Fragment {
...
private ProgressDialog progressDialog;
...
}
progressDialog = ProgressDialog.show(getActivity(), "", getActivity().getResources().getString(R.string.progress_dialog_text), true);
在
private void handleAnnounce() {
pendingAnnounce = false;
Session session = Session.getActiveSession();
if(session == null || !session.isOpened())
return;
List<String> permissions = session.getPermissions();
if(!permissions.containsAll(PERMISSIONS)) {
pendingAnnounce = true;
requestPublishPermissions(session);
return;
}
progressDialog = ProgressDialog.show(getActivity(), "", getActivity().getResources().getString(R.string.progress_dialog_text), true);
AsyncTask<Void, Void, Response> task = new AsyncTask<Void, Void, Response>() {
@Override
protected Response doInBackground(Void... voids) {
HitAction hitAction = GraphObject.Factory.create(HitAction.class);
for(BaseListElement element : listElements) {
element.populateOGAction(hitAction);
}
Request request = new Request(Session.getActiveSession(), POST_ACTION_PATH, null, HttpMethod.POST);
request.setGraphObject(hitAction);
return request.executeAndWait();
}
@Override
protected void onPostExecute(Response response) {
onPostActionResponse(response);
}
};
task.execute();
}
if(progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
在
private void onPostActionResponse(Response response) {
if(progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
if(getActivity() == null)
return;
PostResponse postResponse = response.getGraphObjectAs(PostResponse.class);
if(postResponse != null && postResponse.getId() != null) {
String dialogBody = String.format(getString(R.string.result_dialog_text), postResponse.getId());
new AlertDialog.Builder(getActivity()).setPositiveButton(R.string.result_dialog_button_text, null).setTitle(R.string.result_dialog_title).setMessage(dialogBody).show();
init(null);
}
else
handleError(response.getError());
}