为什么我在更新TextView时会获得Resources $ NotFoundException?

时间:2014-07-29 21:51:56

标签: java android multithreading

我正在为android编写一个简单的秒表应用程序,它只会在按下启动时每秒更新计数器,并使用相同的按钮暂停它。在我的main.xml布局中,我有一个TextView和一个Button,没有别的。 这是我到目前为止所写的内容。

public class MainActivity extends Activity implements View.OnClickListener {

    private Button btnToggle;
    private TextView tvStopwatch;
    public Handler updateStopwatch;

    private enum Status {
        WORKING,
        STOPPED
    }

    private Status stopwatchStatus;

    private void toggleStatus() {
        if (stopwatchStatus == Status.WORKING) {
            stopwatchStatus = Status.STOPPED;
            Stopwatch.getStopwatch().dispose();
            btnToggle.setText("Start");
        }else{
            stopwatchStatus = Status.WORKING;
            Stopwatch.getStopwatch().start();
            btnToggle.setText("Stop");
        }
    }

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

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnToggle:
                toggleStatus();
                break;
        }
    }

    private void initView() {
        btnToggle = (Button) findViewById(R.id.btnToggle);
        tvStopwatch = (TextView) findViewById(R.id.tvStopwatch);

        updateStopwatch = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                tvStopwatch.setText(msg.what);
            }
        };

        Stopwatch.getStopwatch().setHandler(updateStopwatch);
        btnToggle.setOnClickListener(this);

        stopwatchStatus = Status.STOPPED;
    }



    public static class Stopwatch {

        private int timestamp;

        private boolean working;

        private Handler updateStopwatch;

        private Stopwatch() {

        }

        private static Stopwatch stopwatch;

        public static Stopwatch getStopwatch() {

            if (stopwatch == null) {
                stopwatch = new Stopwatch();
            }
            return stopwatch;
        }

        public void setHandler(Handler updateStopwatch){
            this.updateStopwatch = updateStopwatch;
        }

        public void start() {

            if (!working) {

                working = true;

                Thread tick = new Thread() {
                    public void run() {
                        while (working) {
                            try {
                                sleep(1000);
                            } catch (InterruptedException ex) {
                                ex.printStackTrace();
                            } finally {
                                timestamp++;
                                updateStopwatch.sendEmptyMessage(timestamp);
                            }
                        }
                    }
                };

                tick.start();
            }
        }

        public void dispose() {
            working = false;
        }
    }
}

重点是,我希望我的秒表工作在它的独立线程中,并且在我的主UI线程中有一个处理程序,它将继续更新TextView,但是当我调试我的应用程序时,它会不断抛出异常在这一行

  

tvStopwatch.setText(msg.what);

我对Handlers很新,所以我想我只是缺少一些基本的东西。 这也是异常的日志

android.content.res.Resources$NotFoundException: String resource ID #0x1
            at android.content.res.Resources.getText(Resources.java:1057)
            at android.widget.TextView.setText(TextView.java:4186)
            at unisoftdevelopment.com.stopwatch.MainActivity$1.handleMessage(MainActivity.java:61)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5279)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

请提前感谢,请帮助找出问题所在。

1 个答案:

答案 0 :(得分:4)

要犯的常见错误:setText()有一个带整数的重载(必须是字符串资源ID,例如R.string.my_string)。

收到消息时:

tvStopwatch.setText(msg.what);

它正在尝试将msg.what解析为字符串资源,并在无法找到时崩溃。您应该将时间戳转换为字符串,而是使用它:

tvStopwatch.setText(String.valueOf(msg.what));