【问题标题】:Trying to make a TextView clock, app freezes on launch尝试制作 TextView 时钟,应用程序在启动时冻结
【发布时间】:2014-01-01 20:22:56
【问题描述】:

我试图简单地提取系统时间,并将其输入到 TextView,等待一秒钟,然后更新它。我为此使用了一个while循环,但是当应用程序启动时,它会冻结,没有显示。难道我做错了什么?也许更好的方法来做到这一点?我需要该方法能够提取整数,以便我可以将它们匹配到设置整数,我希望添加一个警报功能。

MainActivity.java:

package com.example.polyphaser;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timeUpdate();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void timeUpdate() {
        boolean loop = true;

        while (loop = true) {
            String currentTimeString = new SimpleDateFormat("HH:mm:ss").format(new Date());

            // textView is the TextView view that should display it
            TextView sysTime = (TextView) findViewById(R.id.timeDisplay);
            sysTime.setText(currentTimeString);
            Thread.sleep(1000);
        }
    }
}

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/timeDisplay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="50sp" />

</LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android time while-loop textview sleep


    【解决方案1】:

    这显示了如何使用处理程序:

    public class MainActivity extends Activity {
    
        private Handler handler = new Handler();
        private Runnable runnable;
    
        private TextView sysTime;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            sysTime = (TextView) findViewById(R.id.timeDisplay);
    
            runnable = new Runnable() {
                @Override
                public void run() {
                    timeUpdate();
                    handler.postDelayed(this, 1000);
                }
            };
    
        }
    
        @Override
        protected void onResume() {
            handler.postDelayed(runnable, 1000);
            super.onResume();
        }
    
        @Override
        protected void onPause() {
            handler.removeCallbacks(runnable);
            super.onPause();
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        public void timeUpdate() {
            String currentTimeString = new SimpleDateFormat("HH:mm:ss").format(new Date());
    
            // textView is the TextView view that should display it
            sysTime.setText(currentTimeString);
        }
    }
    

    【讨论】:

    • 是的,这行得通,谢谢!如果可以,你认为你能解释为什么会这样吗?
    • 我的英语不足以解释这一点。在互联网上搜索句柄和可运行文件。你可以开始here
    • 请始终标记解决了您的问题的任何响应,以便将线程正确标记为“已回答”。
    【解决方案2】:

    更好的方法是使用处理程序。

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            sysTime.setText(...
        }
    }, 1000);
    

    我很确定 while 循环会锁定您的主线程,这就是它冻结的原因。 在开发页面查看处理程序。 http://developer.android.com/reference/android/os/Handler.html

    【讨论】:

      【解决方案3】:

      你需要将 while 循环移动到它自己的线程中,到目前为止你在 UI 线程上睡眠,这意味着所有 UI 绘图的东西都被阻塞了。当您需要在第二个计数线程中更新 UI 时,调用 Activity.runInUIThread(new Runnable(...){...}) 发布更新。

      【讨论】:

        【解决方案4】:

        大问题错了,看你代码中的这一行:

        while (loop = true)
        

        我猜编译器会优化该语句(loop 已经为 true,loop = true 不会评估为布尔值,此外,任何东西都无法访问 loop 以将其值更改为 false),所以你有一个while (false) 导致循环体永远不会执行。

        • 您应该在另一次读取中发生该更新,并使用 runonuithread 更新 textview。

        【讨论】:

          猜你喜欢
          • 2013-02-24
          • 2014-05-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-18
          相关资源
          最近更新 更多