EditText是系统组件,不能在主线程之外进行操作,所以要用Handler来更新

解决方案 »

  1.   


    恩,我看了那个可是在更新呢。。
    你看这个代码
    public class ClicktextApp extends Activity {
    private Thread myRefreshThread = null;
    private ClickText ct = null;
    protected static final int GUIUPDATEIDENTIFIER = 0x101;// 自定义Handler 继随自Handler,需要实现handleMessage方法,用以接受消息!
    Handler myHandler = new Handler() {
    public void handleMessage(Message msg) {//处理消息
    switch (msg.what) {
    case ClicktextApp.GUIUPDATEIDENTIFIER:
    ct.invalidate();// 更新视图UI
    break;
    }
    super.handleMessage(msg);
    }
    };/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ct = new ClickText(this);///////自定义的VIEW  
    setContentView(ct);
    new Thread(new myThread()).start();// 启动线程}class myThread implements Runnable {
    public void run() {
    while (!Thread.currentThread().isInterrupted()) {Message message = new Message();// 生成消息,并赋予ID值
    message.what = ClicktextApp.GUIUPDATEIDENTIFIER;
    myHandler.sendMessage(message);// 投递消息
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    }
    }
    }
    }}
    他的界面自己定义的ClickText类,不可以用我的那个 setContentView(R.layout.main);   
    这怎么办啊。求教。。
      

  2.   

    android中线程不能访问ui主线程中的对象,它认为这个是不安全的,要用handle去通知更新
      

  3.   

    你的ClickText究竟是什么?可以在使用setContentView(R.layout.main)后,再往里面添加自定义的View的,我给你一段代码ClickText ct = new ClickText(this);
    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
                                RelativeLayout.LayoutParams.FILL_PARENT);this.addContentView(view, params);
    可以用上面的方法往界面里动态添加View组件,剩下的你应该可以自己揣摩了
      

  4.   


    我改好了感觉代码没问题。。问题就在这怎么还是不行,
    package com.love;import java.util.Timer;
    import java.util.TimerTask;
    import com.love.R;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.widget.EditText;
    import android.widget.TextView;public class Time extends Activity {
    private EditText EditText01;
    private TextView TextView01;
    private int add = 0;
    private Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
    switch (msg.what) {
    case 1:
    updateTitle();
    break;
    }
    };
    }; @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    EditText01 = (EditText) findViewById(R.id.EditText01);
    TextView01 = (TextView) findViewById(R.id.TextView01);
    new Thread(new myThread()).start();
    }
    class myThread implements Runnable {
    public void run() {
    while (true) {
    Message message = new Message();
    message.what = 1;
    mHandler.sendMessage(message);
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    }
    }
    }
    }
    public void updateTitle() {
    TextView01.setText(add);//这句不行。运行就 The application has stopped unexpectedly..
    add++;
    }怎么办我好难过。我这一次这么纠结、、、
    }