Android:初始化数组

时间:2016-06-14 10:15:03

标签: android arrays

我试图用从webService检索到的字符串来填充列表视图,看起来我的数组最初是空的我知道我应该初始化它但是任何正文都可以给我一个提示如何?这是我的代码:

public class TunisAirports extends Fragment {
    ListView List;
    String[] Name;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_tunis_airports, container, false);

        List = (ListView) view.findViewById(R.id.TNairp);
        final ArrayList<Airport> listAirport = new ArrayList<Airport>();
        for (int i = 0; i < listAirport.size(); i++) {
            Airport airport = listAirport.get(i);
            String AirName = airport.getname();
        }

        String Airport_URL = AppConfig.URL_GET_AIRPORT;
        Log.d("URL Airport", Airport_URL);
        String tag_string_req = "airport";
        StringRequest strReq = new StringRequest(Request.Method.GET,
            Airport_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONArray jo = new JSONArray(response);
                        for (int i = 0; i < jo.length(); i++) {
                            JSONObject jObj = jo.getJSONObject(i);
                            Airport new_airport = new Airport();
                            new_airport.setname(jObj.getString("airport_name"));

                            Name[i] = new_airport.getname();
                        }


                        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                            getActivity().getApplicationContext(),
                            R.layout.row_airport,
                            R.id.lbl_name,
                            Name);

                        List.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("dynamic Error: ", error.getMessage());
                }
            });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq,
            tag_string_req);
    }
}

我的模特:

public class Airport {

    @SerializedName("name")
    public String name;

    @SerializedName("city")
    public String city;

    @SerializedName("country")
    public String country;

    @SerializedName("iata")
    public String iata;

    @SerializedName("icao")
    public String icao;

    @SerializedName("latitude")
    public String latitude;

    @SerializedName("longitude")
    public String longitude;

    @SerializedName("altitude")
    public String altitude;

    @SerializedName("timezone")
    public String timeZone;

    @SerializedName("dst")
    public String dst;

    @SerializedName("Image")
    public String Image;

    @SerializedName("Description")
    public String Description;

    @SerializedName("id")
    public String id;

    public String getname() {
        return name;
    }

    public void setname(String name) {
        this.name = name;
    }

    public String getImage() {
        return Image;
    }

    public void setImage(String image) {
        this.Image = image;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        this.Description = description;
    }

    public String getid() {
        return id;
    }

    public void setid(String id) {
        this.id = id;
    }
}

我在这一行得到NullPointerException

  

Name [i] = new_airport.getname();

1 个答案:

答案 0 :(得分:1)

正如我所看到的,jo.length()没有对null或任何内容的条件检查,你可以直接使用它来初始化你的数组

String[] Name = new String[jo.length()]; 

另一种方法是使用List<String> liAirportName,在此你不必担心长度,你可以简单地做到

List<String> liAirportName = new ArrayList<String>();
liAirportName.add(new_airport.getname());

如果您希望直接使用liAirportName或者将其转换为数组,则可以添加所有机场名称

String[] Name = liAirportName.toArray(new String[liAirportName.size()]);