2013-05-18 47 views
0

am使用應用程序的超時函數使用可運行的thread.this超時函數在單獨的類中。如何在android中的可運行線程中顯示對話框?

我在活動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 [email protected] 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) 

回答

3

使用Activity.runOnUiThread用於顯示從非UI線程對話框。做到這一點是:

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

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

    } 
}; 
+0

@ρяσѕρєя中創建它再次顯示相同的錯誤。 – Yugesh

+0

@Yugesh:如果你得到錯誤,然後PLZ也添加日誌與問題 –

+0

@ρяσѕρєяK我添加日誌貓錯誤消息。 – Yugesh

0

不能顯示Activity一個對話框,而運行Activity是B(A暫停)。該對話框應該在Activity B

+0

如何在活動B中創建該對話框 – Yugesh

相關問題