从另一个类访问GUI

时间:2014-03-08 16:42:43

标签: android user-interface

xml代码

 <RelativeLayout 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: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="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:id="@+id/linearLayout1"
            android:orientation="horizontal" >

        </LinearLayout>


    </RelativeLayout>

java代码

 public class MainActivity extends Activity {

        public LinearLayout layout;


        @SuppressWarnings("deprecation")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            layout = (LinearLayout) findViewById(R.id.linearLayout1);

            TableLayout table = new TableLayout(this);
            int k = 0;
            for (int i = 0; i < boardSize; i++) {

                TableRow row = new TableRow(this);

                for (int j = 0; j < boardSize; j++) {

                    TextView cell = new TextView();
                    cell.setBackgroundColor(Color.BLACK);
                    cell.setId(k);
                    k++;

                    cell.setHeight(20);
                    cell.setWidth(20);


                    row.addView(cell);

                }

                table.addView(row);
            }

            layout.addView(table);  

    }

在没有得到“android.view.ViewRootImpl $ CalledFromWrongThreadException的情况下,从另一个类做layout.findViewById(id).setBackgroundColor(Color.WHITE);的麻烦方法是什么?只有创建视图层次结构的原始线程才能触及其视图。” 要么 “I / Choreographer(31253):跳过X帧!应用程序可能在其主线程上做了太多工作。”

timer.schedule(new TimerTask() {
    public void run() {
        move();
        runOnUiThread(new Runnable() {
            public void run() {
                try {
                    layout.findViewById(id).setBackgroundColor(Color.BLACK);
                } catch (Exception e) {
                    System.out.println("Error 2 " + e);
                }
            }, 0, 300);

这就是我尝试更新细胞的方式。

03-08 13:16:44.869: D/libEGL(677): loaded /system/lib/egl/libEGL_tegra.so
03-08 13:16:44.889: D/libEGL(677): loaded /system/lib/egl/libGLESv1_CM_tegra.so
03-08 13:16:44.889: D/libEGL(677): loaded /system/lib/egl/libGLESv2_tegra.so
03-08 13:16:44.919: D/OpenGLRenderer(677): Enabling debug mode 0
03-08 13:16:45.489: I/System.out(677): Error 1 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-08 13:16:45.489: I/System.out(677): Error 1 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-08 13:16:45.499: I/System.out(677): Error 1 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

1 个答案:

答案 0 :(得分:0)

您必须在非UI线程的其他地方调用layout.findViewById(id).setBackgroundColor(Color.WHITE);

您可以将视图对象传递给想要设置背景颜色的类,并执行:

view.post(new Runnable() {
            @Override
            public void run() {
                view.setBackgroundColor(Color.WHITE);
            }
        });