mensa23 2014-03-09 16:48 采纳率: 0%
浏览 1905

关于handle thread的错误

打开程序当第一次进入该Activity调用线程能够显示列表,但是当返回之后第二次进入Activity就会报错Only the original thread that created a view hierarchy can touch its views.

代码如下:

public class ChatActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private String fromActivityName;
private boolean fresh = false;
private String val = null;

private Button mBtnSend;
private Button mBtnBack;
private EditText mEditTextContent;
private ListView mListView;
private ChatMsgViewAdapter mAdapter;
private List<ChatMsgEntity> mDataArrays = new ArrayList<ChatMsgEntity>();

public static String sendTime;
public static String content;
public static int sendType;

private List<String> sendTimeList = new ArrayList<String>();
private List<String> contentList = new ArrayList<String>();
private List<Integer> sendTypeList = new ArrayList<Integer>();

private String url = ...

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat_xiaohei);
    Intent fromIntent = getIntent();
    fromActivityName = fromIntent.getStringExtra(HygTool.formActivityName);

    initView();

    new Thread(runnable).start();
}

Handler handler =  new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        Bundle data = msg.getData();
        val = data.getString("value");
        Log.i("mylog","请求结果-->" + val);
        try {
            if (msg.what==0){
                Log.i("mylog","初始化列表!" );
                initData(false);
            }else{
                Log.i("mylog","刷新列表!" );
                initData(true);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
};

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        refresh();
    }
};

private void refresh() {
    String result = null;
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    HttpResponse response = null;
    try {
        response = httpClient.execute(request);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        result = EntityUtils.toString(response.getEntity());
    } catch (IOException e) {
        e.printStackTrace();
    }
    //JSONObject object = new JSONObject(result);
    Log.i("result", result);
    Message msg = new Message();
    Bundle data = new Bundle();
    data.putString("value",result);
    msg.what = 1;
    if (!fresh){
        msg.what=0;
    }else{
        msg.what=1;
    }
    msg.setData(data);
    handler.handleMessage(msg);
}

public void initData(boolean fresh) throws JSONException {
    JSONArray array = new JSONArray(val);
    Log.i("array", array.toString());
    for (int i = 0; i < array.length(); i++) {
        JSONObject jsonItem = array.getJSONObject(i);
        sendTime = jsonItem.getString("sendTime");
        content = jsonItem.optString("content");
        sendType = jsonItem.getInt("sendType");
        sendTimeList.add(sendTime);
        contentList.add(content);
        sendTypeList.add(sendType);
        Log.i("scs1",sendTime+content+sendType);
    }
    Log.i("num", String.valueOf(sendTimeList.size()));
    for (int j = 0; j < sendTimeList.size(); j++){
        ChatMsgEntity entity = new ChatMsgEntity();
        entity.setDate(sendTimeList.get(j));
        entity.setName(Tool.username);
        entity.setText(contentList.get(j));
        mDataArrays.add(entity);
    }
    Log.i("num", String.valueOf(mDataArrays.size()));
    mAdapter = new ChatMsgViewAdapter(this, mDataArrays);
    mListView.setAdapter(mAdapter);
}
  • 写回答

1条回答 默认 最新

  • -小柚子 2014-03-17 02:55
    关注

    startActivity的时候intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)试试?

    评论

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn