0

**如何在数组适配器中的线程之外访问 stringArray。在下面的代码中,stringArray 是在线程内声明的,该线程将在下面的数组适配器中被接受。

当我们尝试运行代码时,它会抛出一个空异常,在数组适配器中提供一个空数组。(请关注...标记的代码)

source = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1)
 dest = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView2);

    readStopName();

                   **adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,stringArray);**
    //how to access the stringArray outside a Thread in array adapter 

                   source.setAdapter(adapter);
                      dest.setAdapter(adapter);
                   source.setThreshold(2);
                   dest.setThreshold(2);
                addListenerOnButton();
            }

            public void readStopName() 
            {
                new Thread() 
                {
                    public void run() 
                    {
                        // TODO Run network requests here.


                        /* try */
                        try 
                        {
                            readFeed = builder.toString();
                            json = new JSONObject(readFeed);
                            JSONArray jsonArray = new JSONArray(
                                    json.optString("ResultSet"));
                            System.out.println("OKAY_4!!");

                         stringArray = new ArrayList<String>();
                            //JSONArray jsonArray = new JSONArray();
                            for(int i = 1, count = jsonArray.length(); i< count; i++)
                            {
                                try 
                                {
                                     json = jsonArray.getJSONObject(i);
                                     System.out.println("OKAY!!" +json.toString());
                                    **stringArray.add(json.toString());**
                                   //how to access the stringArray outside a Thread in array adapter 

                                }

                                catch (JSONException e) 
                                {
                                    e.printStackTrace();
                                }
                            } 

                        }
                         catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }.start();
                System.out.println("OKAY!!" + builder.toString());


            }
4

0 回答 0