【问题标题】:Why do I get "FATAL EXCEPTION: Thread-2723" when adding text to a String.format?向 String.format 添加文本时,为什么会出现“FATAL EXCEPTION: Thread-2723”?
【发布时间】:2015-11-16 17:27:57
【问题描述】:

我有这个 Runnable 计时器线程:

private Runnable updateTimerThreadRecording = new Runnable() {

        public void run() {

            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

            updatedTime = timeSwapBuff + timeInMilliseconds;

            int secs = (int) (timeInMilliseconds / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int hours = mins / 60;
            mins = mins % 60;
            String timer = "Recording Time: " + String.format("%02d", hours) + ":" + String.format("%02d", mins) + ":" + String.format("%02d", secs);
            timerValueRecord.setText(timer);
            customHandler.postDelayed(this, 1000);
        }

    };

我添加的是:“录制时间:” 字符串计时器的原行是:

String timer = "" + String.format("%02d", hours) + ":" + String.format("%02d", mins) + ":" + String.format("%02d", secs);

但是由于我添加了录制时间: 我得到了这个例外。我尝试在“%02d”之前的 String.format 中添加此文本,但同样的例外。

没有它作为原始行也不例外。 没有我添加的文本,它运行良好,我看到的是:00:00:00,它正在计算秒数。但我现在想添加一个文本,使其在 timerValueRecord TextView 上看起来像这样: Recording Time: 00:00:00

这是 Logcat:

11-16 19:18:46.505  20569-20753/com.test.webservertest E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-2723
    Process: com.test.webservertest, PID: 20569
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
            at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6334)
            at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:890)
            at android.view.View.requestLayout(View.java:17407)
            at android.view.View.requestLayout(View.java:17407)
            at android.view.View.requestLayout(View.java:17407)
            at android.view.View.requestLayout(View.java:17407)
            at android.view.View.requestLayout(View.java:17407)
            at android.view.View.requestLayout(View.java:17407)
            at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)
            at android.view.View.requestLayout(View.java:17407)
            at android.widget.TextView.checkForRelayout(TextView.java:7054)
            at android.widget.TextView.setText(TextView.java:4186)
            at android.widget.TextView.setText(TextView.java:4044)
            at android.widget.TextView.setText(TextView.java:3999)
            at com.test.webservertest.MainActivity$1.run(MainActivity.java:230)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at com.test.webservertest.MainActivity$3.run(MainActivity.java:272)
            at java.lang.Thread.run(Thread.java:818)

【问题讨论】:

标签: android


【解决方案1】:

您正在尝试修改另一个线程(您的 Runnable)中的视图字段,Android 不接受这一点,您需要将此更改委托给您在 UI 上使用的线程。问题不在于String.format 在于timerValueRecord.setText(timer); 您可以尝试使用runOnUiThread 将您的runnable 发布到那里。到目前为止,我不知道这样做的性能问题,但要小心。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多