1

我正在使用使用可运行线程的应用程序的超时功能。此超时功能在单独的类中。

我在活动 A 中调用此函数,但我在活动 B 中意味着它显示错误,如错误令牌异常。

超时功能类

 public class Timeout_function{

private Handler mHandler;
Activity activity;
String dialog_msgs,Avaliable_quantity;
boolean isShown = false,Connection;

Internet_connection_checking int_chk;

Json_response json_res_class = new Json_response();
Dialog dialog;

DecimalFormat df;

double vat_2;

double packing_charge_7;

double Grand_total;

double sub_total = 0, jk;

int quantity;

static JSONObject json = new JSONObject();
static JSONArray jsonarray;

EditText ettot,vat_2per,packing_charge_7per,Grand_totall,Grand_total_ftr;
TextView order_count;

public Timeout_function(Activity activity,Handler mHandler) {
    super();
    this.activity = activity;
    this.mHandler = mHandler;

 }

Runnable first_Task = new Runnable() {
    public void run() {

        onStart_extend();
    }
};

Runnable second_Task = new Runnable() {
    public void run() {

            dialog_msgs = "First";
            dialogs();
    mHandler.removeCallbacks(first_Task);

    onStop();   

}
};

public void onStart() {
    mHandler.postDelayed(first_Task, 1 * 30 * 1000);
}

public void onStart_extend(){
    mHandler.postDelayed(second_Task, 1 * 30 * 1000);
}

public void onStart_user_extend(){
    mHandler.postDelayed(second_Task, 1 * 30 * 1000);
}


//Error Messages

public void dialogs(){

Typeface font = Typeface.createFromAsset(activity.getAssets(), "fonts/CALIBRI.TTF");;
dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.setContentView(R.layout.dialogs);
dialog.setCancelable(false);
isShown=true;

TextView tv_msg = (TextView)dialog.findViewById(R.id.dialog_texts);
tv_msg.setTypeface(font);
tv_msg.setText(""+dialog_msgs);

Button btn_ok=(Button)dialog.findViewById(R.id.btn_dialogs_ok);
btn_ok.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        dialog.dismiss();
    }
});
dialog.show();
}
}

任何人都可以帮助我解决这个问题。

我的日志猫错误:

  05-18 13:20:14.983: E/AndroidRuntime(7672): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@42f2d668 is not valid; is your activity running?
  05-18 13:20:14.983: E/AndroidRuntime(7672):   at android.view.ViewRootImpl.setView(ViewRootImpl.java:692)
  05-18 13:20:14.983: E/AndroidRuntime(7672):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:345)
  05-18 13:20:14.983: E/AndroidRuntime(7672):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
  05-18 13:20:14.983: E/AndroidRuntime(7672):   at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
  05-18 13:20:14.983: E/AndroidRuntime(7672):   at android.view.Window$LocalWindowManager.addView(Window.java:556)
  05-18 13:20:14.983: E/AndroidRuntime(7672):   at android.app.Dialog.show(Dialog.java:277)
  05-18 13:20:14.983: E/AndroidRuntime(7672):   at tab.FA.V3.Timeout_function.dialogs(Timeout_function.java:263)
 05-18 13:20:14.983: E/AndroidRuntime(7672):    at tab.FA.V3.Timeout_function$2$1.run(Timeout_function.java:80)
 05-18 13:20:14.983: E/AndroidRuntime(7672):    at android.app.Activity.runOnUiThread(Activity.java:4741)
 05-18 13:20:14.983: E/AndroidRuntime(7672):    at tab.FA.V3.Timeout_function$2.run(Timeout_function.java:75)
 05-18 13:20:14.983: E/AndroidRuntime(7672):    at android.os.Handler.handleCallback(Handler.java:615)
 05-18 13:20:14.983: E/AndroidRuntime(7672):    at android.os.Handler.dispatchMessage(Handler.java:92)
 05-18 13:20:14.983: E/AndroidRuntime(7672):    at android.os.Looper.loop(Looper.java:137)
 05-18 13:20:14.983: E/AndroidRuntime(7672):    at android.app.ActivityThread.main(ActivityThread.java:4895)
 05-18 13:20:14.983: E/AndroidRuntime(7672):    at java.lang.reflect.Method.invokeNative(Native Method)
 05-18 13:20:14.983: E/AndroidRuntime(7672):    at java.lang.reflect.Method.invoke(Method.java:511)
 05-18 13:20:14.983: E/AndroidRuntime(7672):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
 05-18 13:20:14.983: E/AndroidRuntime(7672):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
 05-18 13:20:14.983: E/AndroidRuntime(7672):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

3

使用Activity.runOnUiThread从非 ui 线程显示 DialogBox。这样做:

Runnable second_Task = new Runnable() {
    public void run() {
    activity.runOnUiThread(new Runnable() {
        public void run() {
        dialog_msgs = "First";

            //show dialog here..
            dialogs();
        }
    });

    }
};
于 2013-05-18T07:27:11.830 回答
0

Activity运行Activity为 B(A 已暂停)时,您无法在 A 中显示对话框。该对话框应在ActivityB中创建

于 2013-05-18T22:46:21.543 回答