从另一堂课访问GUI



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.view.view.view.view.view.view.view.view.view.view.view.viewrongthreadexception:只有创建视图层次结构的原始线程才能触摸其视图"的情况下,从另一个类中进行layout.findViewById(id).setBackgroundColor(Color.WHITE);的bast方式是什么。或者" I/编舞者(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.

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

您可以将视图对象传递到想要设置背景颜色并做的类:

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

最新更新