android.view.WindowManager $ BadTokenException:无法添加窗口 -

时间:2013-04-27 12:32:56

标签: android android-activity android-windowmanager

我们正在运行一个长时间运行的应用程序(运行超过5小时)来跟踪通过GPS的路径。 我们不会随着时间的推移增加任何数据。 使用一些全局变量来保持距离和时间。 但是当我们试图显示对话框时,我们会崩溃,但很难再现。 这是堆栈跟踪

    android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@406ea4e0 is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:532)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at eventHandlers.SavariMapActivityEventHandler.HandleEndTrip(SavariMapActivityEventHandler.java:104)
at eventHandlers.SavariMapActivityEventHandler.onClick(SavariMapActivityEventHandler.java:50)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)

我看到了这些帖子

Link 1 Link 2

以及许多其他解决方案,每个人都告诉检查activity.isFinishing(),甚至我没有使用getApplicationContext()来显示对话框。 当我们没有打电话给finish()时,很奇怪这很开心。请让我知道你的想法。

用于显示单击按钮的对话框的Java代码:

单击按钮

时调用的HandleEndTrip()方法

对于有关活动的所有eventhandeler,我创建了一个java类

public class SavariMapActivityEventHandler extends EventHandler {
    private static SavariMapActivityEventHandler instance = null;

    @Override
    public void handleClick(Activity activity) {
        setActivity(activity);
        INITClick();
    }

    @Override
    public void INITClick() {
        getActivity().findViewById(R.id.endtrip).setOnClickListener(this);
        getActivity().findViewById(R.id.expensebutton).setOnClickListener(this);
        getActivity().findViewById(R.id.details).setOnClickListener(this);

    }

    private SavariMapActivityEventHandler() {


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.endtrip:
            HandleEndTrip();
              break;

        }
    }

    private void HandleEndTrip() {
        AlertDialog alertDialog = new Builder(getActivity()).create();
        alertDialog.setTitle("Confirmation");
        alertDialog.setMessage("Has the trip ended?");
        alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                arg0.dismiss();
            }
        });
        alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                try {
                    getActivity().endthread = true;
                    getActivity().getHandler().removeCallbacksAndMessages(null);
                    TimeCalculator calculator = new TimeCalculator(
                            getActivity(), System.currentTimeMillis(), holder
                                    .getStartfromGarageTime());
                    CustomerInfo.getInstance().setEndduration(
                            calculator.getHour());
                    getActivity().finish();
                    SavariMapActivityEventHandler.getInstance()
                            .finishActivity();
                    getActivity().startActivity(
                            new Intent(getActivity(),
                                    BillingConfirmActivity.class));

                }
                catch (Exception e)
                {
                    SavariUserSettings.appendLog("Savaari MapAcivityEventHandeler", ""+e+" "+e.getStackTrace()[0].getLineNumber());                 
                }
            }
        });

        alertDialog.show();

    }

    @Override
    public SavariMapActivity getActivity() {
        return (SavariMapActivity) super.getActivity();
    }

    public static SavariMapActivityEventHandler getInstance()
    {
        if (instance == null)
            instance = new SavariMapActivityEventHandler();
        return instance;
    }

    public static void setInstance(SavariMapActivityEventHandler instance) {
        SavariMapActivityEventHandler.instance = instance;
    }
}

EventHandler的代码:

    public abstract class EventHandler implements OnClickListener {
private ProgressDialog dialog;
public final String SETTINGS_PREFERENCE = "setting_pref";
private Activity activity;

public abstract void handleClick(Activity activity);

public abstract void INITClick();

public ValueHolder holder = null;

protected final void setActivity(Activity activity) {
    this.activity = activity;
    holder = new ValueHolder(activity);

}

public final void finishActivity() {
    activity.finish();
}

public final void INITDialog(String title, String mesg) {
    dialog = new ProgressDialog(activity);
    dialog.setTitle(title);
    dialog.setMessage(mesg);
    dialog.show();
}

public final void finishDialog() {
    try {
        if (dialog != null)
            dialog.dismiss();
        } catch (Exception e)
        {

        }
}

public Activity getActivity() {
    return activity;
}

}

1 个答案:

答案 0 :(得分:0)

在启动警告对话框之前,请检查活动是否正在完成

if(!isFinishing()){
   // create and show dialog
}