android 异常处理:android.view.viewroot$calledfromwrongthreadexception

来源:互联网 发布:淘宝宝贝描述图片制作 编辑:程序博客网 时间:2024/06/05 14:13
在写线程时 
 子线程中是不能做任何数据更新的  。
private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what ==20){
//界面在handle中更新即可解决该异常 
                adapter = new ProductsAdapter(getActivity(), list2);

                mListView.setAdapter(adapter);


            }
        }
    };


 new Thread(new Runnable() {
             @Override
             public void run() {
                 Gson gson = new Gson();
                 Type listType = new TypeToken<List<ProductList>>() {
                 }.getType();
            String json = NerService.getServerJsonWithType(NerService.PRODUCTS_URL);
                 System.out.println("json:" + json);
                 List<ProductList> mProductList = gson.fromJson(json, listType);
                 list2 = new ArrayList<HashMap<String, Object>>();
                 for (ProductList list : mProductList) {
                     HashMap<String, Object>  map = new HashMap<String, Object>();
                     map.put("id", list.getId());
                     map.put("name", list.getName());
                     System.out.println("name===>"+list.getName());
                     map.put("jiliangguige", list.getJiliangguige());
                     map.put("baozhuangguige", list.getBaozhuangguige());
                     map.put("jixing", list.getJixing());
                     map.put("baozhuang", list.getBaozhuang());
                     map.put("guige", list.getGuige());
                     map.put("pizhunwenhao", list.getPizhunwenhao());
                     map.put("yibaoleixing", list.getYibaoleixing());
                     map.put("OTC", list.getOTC());
                     map.put("chufangleixing", list.getChufangleixing());
                     map.put("pics", list.getPics());
                     map.put("manufacturer", list.getManufacturer());
                     map.put("m_price", list.getM_price());
                     map.put("category_1", list.getCategory_1());
                     map.put("category_2", list.getCategory_2());
                     list2.add(map);
                 }
            Message message = Message.obtain();
                 message.what =20;
               handler.sendMessage(message);


             }
         }).start();
0 0
原创粉丝点击