Android:Thread和它内部的NotFoundException

时间:2012-09-28 20:28:24

标签: android multithreading

我遇到NotFound资源异常的问题。我确信我的资源ID与我设置的完全相同,但无论如何它都会失败! 这是一件有趣的事情,因为带有“findViewById(R.id.editText1)”的第一行执行得很好,我看到标签“start!”在editText1中,但是Thread内的第二个 - 失败了:

09-29 00:17:45.103: E/AndroidRuntime(347): android.content.res.Resources$NotFoundException: String resource ID #0x0

任何人都可以帮我解决这类问题吗? 这是一个代码:

EditText editText = ( EditText ) findViewById( R.id.editText1 );
editText.setText( "start!" );

final Handler handler = new Handler();
Runnable runnable = new Runnable() {

    @Override
    public void run() {

        for (int i = 0; i <= 10; i++) {

            final int value = i;

            try {

                Thread.sleep( 1000 );

            } catch ( InterruptedException e ) {

                e.printStackTrace();

            }

           handler.post( new Runnable() {

               @Override
               public void run() {

                   EditText editText = ( EditText ) findViewById( R.id.editText1 );
                   editText.setText( value );

               }

           } );

        }

    }

};

Thread thread = new Thread( runnable );
thread.start();

1 个答案:

答案 0 :(得分:1)

value是一个整数值。将整数传递到setText方法将尝试按资源ID从strings.xml文件中查找String。如果要显示数字,则需要将其解析为StringInteger.toString(value)