这个问题折腾了我一个下午,结果还是没解决。
主要代码 :
private void updateBusinessItemInfoList() {
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub if (ShopsActivity.this.businessItemInfoList.size() < 800) {
ShopsActivity.this.businessItemInfoListTemp = ShopsActivity.this
.getBusinessInfo();
} else {
ShopsActivity.this.businessItemInfoListTemp = null;
} try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} Message message = new Message();
message.what = 1;
//ShopsActivity.this.getHandler().sendMessage(message);
ShopsActivity.this.getHandler().sendEmptyMessageDelayed(message.what, 0);
message = null;
} }).start();
}
private Handler createHandler() {
Handler handler = new Handler() { @Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// TODO Auto-generated method stub
switch (msg.what) {
case 1:
if (businessItemInfoListTemp != null) {
//System.out.println("businessItemInfoListTemp---categoryId==0---Thread->"+Thread.currentThread().getId());
//问题出在下面两名
ShopsActivity.this.businessItemInfoList.addAll(businessItemInfoListTemp);
ShopsActivity.this.businessItemAdapter.notifyDataSetChanged();
ShopsActivity.this.loading.setVisibility(View.GONE);
ShopsActivity.this.refreshable = true;
} else {
ShopsActivity.this.loading.setVisibility(View.GONE);
Toast.makeText(ShopsActivity.this,"所有商家都已列出",Toast.LENGTH_LONG).show();
}
break;
case 2:
Intent intent = new Intent();
intent.setClass(ShopsActivity.this, ShopsActivity.class);
ShopsActivity.this.startActivity(intent);
break;
default:
break;
}

}
};
return handler;
}public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
updateBusinessItemInfoList();
}
/**
 * 绑定类别
 */
private void BindCategoryGv()
{
GvCategoryShops = (GridView)findViewById(R.id.GvCategoryShops);
final List<Category> listCat = new CategoryDao().getCategoryList(0);

final List<Map<String,Object>>  listItemsCat = new ArrayList<Map<String,Object>>();
for(Category item:listCat)
{
Map<String,Object> listItem = new HashMap<String,Object>();
listItem.put("categoryName", item.getCat_name());
listItemsCat.add(listItem);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this
, listItemsCat 
, R.layout.category_list
, new String[]{"categoryName"}
, new int[]{R.id.TvCategoryNameCategoryList});
GvCategoryShops.setAdapter(simpleAdapter);
GvCategoryShops.setOnItemClickListener(new OnItemClickListener(){ @Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) {
// TODO Auto-generated method stub
//System.out.println(list.get(position).getCat_id());
categoryId = listCat.get(position).getCat_id();
Intent intent = new Intent();
Bundle extras = new Bundle();
extras.putString("sort", orderBy);
extras.putString("order_title",orderTitle);
extras.putInt("categoryId", categoryId);
extras.putInt("redirctType", R.string.redirctShops);
intent.putExtras(extras);
intent.setClass(ShopsActivity.this, ShopsActivity.class);
ShopsActivity.this.startActivity(intent);
}

});
}

/**
 * 排序
 */
private void BindSortGv()
{
GvSortShops = (GridView)findViewById(R.id.GvSortShops);

final List<Map<String,Object>>  listItems = new ArrayList<Map<String,Object>>();

Map<String,Object> listItem1 = new HashMap<String,Object>();
listItem1.put("categoryName", "按星级排序");
listItems.add(listItem1);
Map<String,Object> listItem2 = new HashMap<String,Object>();
listItem2.put("categoryName", "按点击排序");
listItems.add(listItem2);
Map<String,Object> listItem3 = new HashMap<String,Object>();
listItem3.put("categoryName", "按距离排序");
listItems.add(listItem3);

SimpleAdapter simpleAdapter = new SimpleAdapter(this
, listItems 
, R.layout.category_list
, new String[]{"categoryName"}
, new int[]{R.id.TvCategoryNameCategoryList});
GvSortShops.setAdapter(simpleAdapter);
GvSortShops.setOnItemClickListener(new OnItemClickListener(){ @Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) {
// TODO Auto-generated method stub

// which代表哪个列表项被单击了
switch (position)
{
case 0:
//设置排序方式
orderBy = "score|desc";
orderTitle = "按评分排序";
break;
case 1:
//设置排序方式
orderBy = "click_count|desc";
orderTitle = "按点击排序";
break;
case 2:
//距离排序
orderBy = "distance|desc";
orderTitle = "按点击排序";
break;
}

//将按钮和图标还原
proBarSortTv.setTextColor(color.black4);
proBarIcon1.setImageResource(R.drawable.icon_01);

Intent intent = new Intent();
Bundle extras = new Bundle();
extras.putString("sort", orderBy);
extras.putString("order_title",orderTitle);
intent.putExtras(extras);
intent.setClass(ShopsActivity.this, ShopsActivity.class);
ShopsActivity.this.startActivity(intent);
}

});
}
问题描述:
handle+thread 作异步加载 ,并作了滚动触发加载更多。这些功能本已实现,下午想加个类别搜索条件,程序就出问题了,异常信息是: Only the original thread that created a view hierarchy can touch its views.
网上也查了很久,都是说要更新主线程的ui 需要通过handle。可我程序本就是如此。而且我第一次进来都没有出错,初始化时列表是没有数据的,只是通过updateBusinessItemInfoList()装载进来。 当我加了类别条件用intent 重定向回这个Activity时就出问题异常了,异常是在更新listView时。最郁闷的是我选择排序时却不会出现异常,listView正常更新。 经过我不停测试,发现当返回数据量比较少时(三五条)就会出现异常,当数量多时就不会,就像排序一样,数据是从服务端返回的JSON数据,我是这样测试的:将服务端程序接收的参数屏蔽掉,这样android程序就不会报异常。 
因为第一次打开这个Activity 时不会报异常,我就想到是不是不能自身重定向,所以我又加了个Activity作跳板,结果还是一样。
又想既然第一次打开没问题,那是不是有东西在结束这个Activity时没有关闭,所以我在onStop中把当前线程结束掉,不过还是不行,出现了新的问题。-_- 被这问题搞了一个下午,希望有大神指点下啊,感觉不尽。 下班 回家,晚点再回来。