2 mensa23 mensa23 于 2014.03.10 00:48 提问

关于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个回答

u010353414
u010353414   2014.03.17 10:55

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!