2013-06-24 84 views
0

我有一個AlertDialog顯示,並希望文本倒計時。在嘗試按以下方式實現它時,對話框完全按照需要顯示,但我試圖弄清楚如何在顯示對話框時將其中一個TextView更改爲定時器。當我嘗試以這種方式實現它時,它會崩潰,就像你在日誌信息中看到的那樣。如何在AlertDialog顯示時動態更改文本?

public void startOverlay() 
    { 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setCancelable(false); 
     // Get the layout inflater 
     LayoutInflater inflater = getLayoutInflater(); 
     View boxLayout = inflater.inflate(R.layout.overlay, null); 

     builder.setView(boxLayout); 

     Typeface flatui = Typeface.createFromAsset(getAssets(), "fonts/Flat-UI-Font.ttf"); 
     TextView t1 = (TextView)boxLayout.findViewById(R.id.dialogtext1); 
     final TextView t2 = (TextView)boxLayout.findViewById(R.id.dialogtext2); 
     t1.setTypeface(flatui); 
     t2.setTypeface(flatui); 

     final AlertDialog receivedBox = builder.create(); 
     receivedBox.show(); 

     CountDownTimer timer = new CountDownTimer(6000, 1000) { 

      public void onTick(long millisUntilFinished) { 
       final int seconds = (int) (millisUntilFinished/1000); 
       log("Tick: " + seconds); 
       if (seconds == 0) 
        receivedBox.dismiss(); 

       Handler mHandler = new Handler(); 
       mHandler.post(new Runnable() { 
        @Override 
        public void run() { 

         // executed on the UI thread 
         t2.setText(seconds); 
        } 
       }); 

      } 

      public void onFinish() { 
       receivedBox.dismiss(); 
       log("done!"); 
       beginFirst(); 
      } 
     }.start(); 

    } 

登錄信息:

06-24 12:01:18.977: E/AndroidRuntime(11682): FATAL EXCEPTION: main 
06-24 12:01:18.977: E/AndroidRuntime(11682): android.content.res.Resources$NotFoundException: String resource ID #0x5 
06-24 12:01:18.977: E/AndroidRuntime(11682): at android.content.res.Resources.getText(Resources.java:242) 
06-24 12:01:18.977: E/AndroidRuntime(11682): at android.widget.TextView.setText(TextView.java:3773) 
06-24 12:01:18.977: E/AndroidRuntime(11682): at com.workout.MainActivity$1$1.run(MainActivity.java:163) 
06-24 12:01:18.977: E/AndroidRuntime(11682): at android.os.Handler.handleCallback(Handler.java:615) 
06-24 12:01:18.977: E/AndroidRuntime(11682): at android.os.Handler.dispatchMessage(Handler.java:92) 
06-24 12:01:18.977: E/AndroidRuntime(11682): at android.os.Looper.loop(Looper.java:137) 
06-24 12:01:18.977: E/AndroidRuntime(11682): at android.app.ActivityThread.main(ActivityThread.java:4918) 
06-24 12:01:18.977: E/AndroidRuntime(11682): at java.lang.reflect.Method.invokeNative(Native Method) 
06-24 12:01:18.977: E/AndroidRuntime(11682): at java.lang.reflect.Method.invoke(Method.java:511) 
06-24 12:01:18.977: E/AndroidRuntime(11682): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 
06-24 12:01:18.977: E/AndroidRuntime(11682): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
06-24 12:01:18.977: E/AndroidRuntime(11682): at dalvik.system.NativeStart.main(Native Method) 
+0

你是什麼意思,「因爲我不能改變文本」?編譯錯誤?應用崩潰?應用掛起?開發OS崩潰? –

回答

1

您不能更改或修改UI線程之外的意見。嘗試下面的代碼來更新你的TextView。把這段代碼放在onTick

private Handler mHandler = new Handler(); 
mHandler.post(new Runnable() { 
    @Override 
    public void run() { 
     // executed on the UI thread 
     your_text_view.setText("your string"); 
    } 
}); 
+0

我在onTick()中試過這段代碼,使得我的TextView t2最終被定義爲'不能引用內部類中的非最終變量',而現在當't2.setText(seconds);'內部運行()執行它給我一個'ResourcesNotFoundException't2.setText()錯誤,我做錯了嗎? – AggieDev

+0

你能粘貼日誌嗎? –

+0

你是否在run()方法內設置了任何「字符串」資源?錯誤通常是由於您在資源文件「值」中聲明的任何資源,無法通過項目找到。只需做一件事,點擊Project-> clean。清理完項目後,重建它。有時候,「R.java」文件引用了不在項目中的舊值。 –

相關問題