Yugesh
Yugesh

Reputation: 4092

How to show dialog in runnable thread in android?

am using the timeout function of the application using runnable thread.this time out function in separate class.

am call this function in activity A,but i am in activity B means it shows error like bad token exception.

Time out function class

 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();
}
}

Can any one know help me to solve this problem.

MY log-cat error:

  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)

Upvotes: 1

Views: 7625

Answers (2)

EyalBellisha
EyalBellisha

Reputation: 1924

You can't show a dialog in Activity A while the running Activity is B (A is paused). The dialog should be created in Activity B

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

use Activity.runOnUiThread for showing DialogBox from non ui thread. do it as:

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

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

    }
};

Upvotes: 3

Related Questions