【问题标题】:Cannot Refer non final variable productIdList不能引用非最终变量 productIdList
【发布时间】:2013-08-01 09:24:39
【问题描述】:

On*productIdList.add(p.getId());* 它说的这一行不能在以不同方法定义的内部类中引用非最终变量 productIdlist。

代码如下:

public ArrayList<String> getProductData() {


    ArrayList<String> productIdList = new ArrayList<String>();

    new Thread(new Runnable() {

        public void run() {
            HttpClient httpclient= new DefaultHttpClient();
            GeneralConstans GC = new GeneralConstans();
            // Products will be stated in memory
            HttpPost httpget = new HttpPost(GC.UrlConstants);
            HttpResponse response;
            String result = null;
            try {

                HttpContext ctx = new BasicHttpContext();

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        2);
                httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs,
                        "UTF-8"));

                response = httpclient.execute(httpget, ctx);
                HttpEntity resEntity = response.getEntity();

                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity);
                    JSONArray arr = new JSONArray(result);
                    Gson gson = new Gson();
                    if (arr.length() > 0) {
                        for (int j = 0; j < arr.length(); j++) {
                            Product p = gson.fromJson(arr.getString(j),
                                    Product.class);
                            productIdList.add(p.getId());
                        }                       

                    }

                }

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (SocketException e) {
                /*if (checkAbortStatus(e.getMessage()) == true) {
                    handler.sendEmptyMessage(0);
                }*/
            } catch (IOException e) {
                /*if (checkAbortStatus(e.getMessage()) == true) {
                    handler.sendEmptyMessage(0);
                }*/
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }

        private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {

                super.handleMessage(msg);
            }
        }; 

    }).start();
    return productIdList;

问题是什么,我该如何解决? 提前致谢。

【问题讨论】:

    标签: android multithreading arraylist


    【解决方案1】:

    正如它所说,productIdList 必须是 final 才能在匿名内部类中使用。简单地声明为:

    final ArrayList<String> productIdList = new ArrayList<String>();
    

    还要注意:

    • 您的列表将在您的方法返回后填充
    • ArrayList 不是线程安全的:如果没有适当的同步,调用您的方法的代码很可能不会立即(或根本)看到添加到列表中的新产品。

    【讨论】:

    • 我做到了,但它仍然说同样的话
    • @mstfdz 它应该可以工作 - 也许尝试清理和构建您的项目?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 2019-08-19
    • 2012-01-14
    • 1970-01-01
    • 2010-11-20
    • 2014-12-09
    相关资源
    最近更新 更多