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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制