我们正在运行一个长时间运行的应用程序(运行超过 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)
我看到了这些帖子
和许多其他解决方案,每个人都告诉检查activity.isFinishing(),甚至我没有使用getApplicationContext() 来显示对话框。当我们没有调用finish()时,这很奇怪。请让我知道你的想法。
用于显示单击按钮时的对话框的 Java 代码:
单击按钮时调用的 HandleEndTrip() 方法
对于所有关于活动的事件处理程序,我创建了一个 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;
}
}
事件处理程序代码:
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;
}
}