如何在android中的runnable线程中显示对话框?

时间:2013-05-18 07:11:37

标签: android android-dialog

我正在使用runnable thread的应用程序的超时功能。这个超时功能在单独的类中。

我在活动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();
}
}

任何人都知道帮助我解决这个问题。

我的log-cat错误:

  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)

2 个答案:

答案 0 :(得分: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();
        }
    });

    }
};

答案 1 :(得分:0)

当正在运行Activity为B(A暂停)时,您无法在Activity A中显示对话框。应在Activity B

中创建对话框