当前位置: 首页 > 知识库问答 >
问题:

只有创建视图层次的原始线程才能接触其视图。在android上[重复]

巢安澜
2023-03-14

我只是个初学者,所以请原谅我问了一个可能很愚蠢的问题

我不明白只有创建视图层次结构的原始线程才能触摸其视图的含义。

请有人教我为什么会出现这个错误以及如何解决这个问题。

谢谢你

这是我的课

public class MainActivity extends Activity {
    TextView title;
    Random   random  = new Random();
    int      counter = 1;

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

    private void startingUp() {
        Thread timer = new Thread() { //new thread         
            public void run() {
                Boolean b = true;
                try {
                    do {
                        counter++;
                        title();
                        sleep(1000);
                        title.clearComposingText();

                    }
                    while (b == true);
                } catch (IntruptedException e) {
                    e.printStackTrace();
                }
                finally {
                }
            };
        };
        timer.start();
    }

    public void title() {
        title = (TextView) findViewById(R.id.tvTitle);
        switch (random.nextInt(2)) {
            case 0:
                title.setGravity(Gravity.RIGHT);
                break;
            case 1:
                title.setGravity(Gravity.CENTER);
                break;
            case 2:
                title.setGravity(Gravity.LEFT);
                break;
        }
        title.setTextColor(Color.rgb(random.nextInt(250), random.nextInt(250), random.nextInt(250)));
        title.setTextSize(random.nextInt(55) + 10);
    }
}

这是我的LogCat

02-20 10:53:19.293: I/Adreno200-EGLSUB(5816): <ConfigWindowMatch:2078>: Format RGBA_8888.
02-20 10:53:19.303: D/memalloc(5816): /dev/pmem: Mapped buffer base:0x5c914000 size:14135296 offset:10366976 fd:64
02-20 10:53:19.303: E/(5816): Can't open file for reading
02-20 10:53:19.303: E/(5816): Can't open file for reading
02-20 10:53:19.303: D/OpenGLRenderer(5816): Enabling debug mode 0
02-20 10:53:19.373: D/memalloc(5816): /dev/pmem: Mapped buffer base:0x5db58000 size:3768320 offset:0 fd:67
02-20 10:53:20.143: W/dalvikvm(5816): threadid=11: thread exiting with uncaught exception (group=0x40abc210)
02-20 10:53:20.143: E/AndroidRuntime(5816): FATAL EXCEPTION: Thread-3102
02-20 10:53:20.143: E/AndroidRuntime(5816): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
02-20 10:53:20.143: E/AndroidRuntime(5816):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
02-20 10:53:20.143: E/AndroidRuntime(5816):     at android.view.ViewRootImpl.invalidateChild(ViewRootImpl.java:722)
02-20 10:53:20.143: E/AndroidRuntime(5816):     at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:771)
02-20 10:53:20.143: E/AndroidRuntime(5816):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:4112)
02-20 10:53:20.143: E/AndroidRuntime(5816):     at android.view.View.invalidate(View.java:8639)
02-20 10:53:20.143: E/AndroidRuntime(5816):     at android.view.View.invalidate(View.java:8590)
02-20 10:53:20.143: E/AndroidRuntime(5816):     at android.widget.TextView.setGravity(TextView.java:2538)
02-20 10:53:20.143: E/AndroidRuntime(5816):     at com.example.saikoro.MainActivity.title(MainActivity.java:58)
02-20 10:53:20.143: E/AndroidRuntime(5816):     at com.example.saikoro.MainActivity$1.run(MainActivity.java:36)

共有3个答案

姚建树
2023-03-14

不能使用标题修改文本。clearComposingText() 在线程内,因为您只能从UI线程修改视图。使用处理程序,让他更改文本。

商正浩
2023-03-14

由于标题原因,此例外情况不会出现。clearComposingText()。即使这一行没有用处,我们也可以删除这一行。title()函数出现此异常,因为非UI线程正在尝试修改视图。因此,我们需要在UI线程或处理程序中调用此函数。

private void startingUp() {
        Thread timer = new Thread() { //new thread         
            public void run() {
                boolean b = true;
                try {
                    do {
                        counter++;

                        sleep(1000);
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                  title();
                                //title.clearComposingText();//not useful

                            }
                        });


                    }
                    while (b == true);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally {
                }
            };
        };
        timer.start();
    }
金烨华
2023-03-14

将您的启动更改为此。

 private void startingUp() {
    Thread timer = new Thread() { //new thread         
        public void run() {
            Boolean b = true;
            try {
                do {
                    counter++;
                    title();
                    sleep(1000);

                    runOnUiThread(new Runnable() {  
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub

                        title.clearComposingText();
                    }
                });


                }
                while (b == true);
            } catch (IntruptedException e) {
                e.printStackTrace();
            }
            finally {
            }
        };
    };
    timer.start();
}

不能从非UI线程修改视图。

 类似资料: