android studio:java.lang.NullPointerException:尝试在空对象引用上调用接口方法'int java.util.List.size()'

时间:2018-06-14 11:22:38

标签: android

我的代码出了问题。显示消息:java.lang.NullPointerException:尝试在空对象引用上调用接口方法“int java.util.List.size()”。我的代码是:` 公共类MainActivity extends FragmentActivity实现了GoogleMap.OnMyLocationButtonClickListener,OnMyLocationClickListener,OnMapReadyCallback {

private GoogleMap maps;
ArrayList<LatLng> markerPoints=new ArrayList<>();
MarkerOptions markerOptions;
MarkerOptions markerOpt;
LatLng latLng;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //parapobh sto SupportMapFragment ths Activity_main
    SupportMapFragment fm = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));

    /* lhpsh xarth gia to SupportMapFragment */
    fm.getMapAsync(this);

}

@Override
public void onMapReady(GoogleMap googleMap) {
    maps = googleMap;
    LatLng Greece = new LatLng(37.97, 23.73);
    //googleMap.addMarker(new MarkerOptions().position(Greece).title("Marker in Greece"));
    //googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Greece,16));
    Button btn_find = (Button) findViewById(R.id.btn_find);
    OnClickListener findClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText etLocation = (EditText) findViewById(R.id.et_location);
            String location = etLocation.getText().toString();

            if (location != null && !location.equals("")) {
                new GeocoderTask().execute(location);
            }
        }
    };
    btn_find.setOnClickListener(findClickListener);
    Button btn_go=(Button) findViewById(R.id.btn_go);
    OnClickListener goClickListener=new OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText etDieuthinsh=(EditText) findViewById(R.id.et_dieuthinsh);
            String dieuthinsh=etDieuthinsh.getText().toString();
            if(dieuthinsh!=null && !dieuthinsh.equals("")){
                new GeocoderTask1().execute(dieuthinsh);
            }
        }
    };
    btn_go.setOnClickListener(goClickListener);
}
private class GeocoderTask extends AsyncTask<String,Void,List<Address>> {
    @Override
    protected List<Address> doInBackground(String... locationName) {
        Geocoder geocoder = new Geocoder(getBaseContext());
        List<Address>addresses=null;
            try {
                addresses = geocoder.getFromLocationName(locationName[0], 8);
            } catch (IOException e) {
                e.printStackTrace();
            }

        return addresses;
    }

    @Override
    protected void onPostExecute(List<Address>addresses) {

       if (addresses==null||addresses.size()==0) {
         Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
        }

       maps.clear();

        //try {
            for (int t = 0; t < addresses.size(); t++) {
                Address address = (Address) addresses.get(t);
                latLng = new LatLng(address.getLatitude(), address.getLongitude());

                String addressText = String.format("%s,%s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", address.getCountryName());

                markerOptions = new MarkerOptions();
                markerOptions.position(latLng);

                /**
                 * for the start location,the color of marker is Green and*
                 * for the end location, the color of marker is Red
                 */

                markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));

                //prosthesh neou deikth sto GoogleMaps Android API V2
                markerOptions.title(addressText);
                maps.addMarker(markerOptions);

                if (t == 0)
                    maps.animateCamera(CameraUpdateFactory.newLatLng(latLng));

                if (markerPoints.size() == 1) {
                    //lhpsh URL sto Google Directions API
                    String url = getDirectionsUrls(markerPoints.get(0));

                    DownloadTask downloadTask = new DownloadTask();

                    //xekinhste thn lhpsh dedomenwn json apo to Google Directions API
                    downloadTask.execute(url);
                }
            }
        //} catch(NullPointerException e){
        //   e.printStackTrace();
       // }
        }
}
         private class GeocoderTask1 extends AsyncTask<String, Void, List<Address>> {
             @Override
             protected List<Address> doInBackground(String... dieuthinshName) {
                 Geocoder geocoders = new Geocoder(getBaseContext());
                 List<Address> dieuts = null;
                 try {
                  dieuts = geocoders.getFromLocationName(dieuthinshName[0], 8);
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
                 return dieuts;
             }

             @Override
             protected void onPostExecute(List<Address> dieuts) {
                if (dieuts == null || dieuts.size() == 0) {
                    Toast.makeText(getBaseContext(), "No Dieuthinsh found", Toast.LENGTH_SHORT).show();
                }
                 maps.clear();

                 //try{
                for (int k = 0; k < dieuts.size(); k++) {
                     Address address1 = (Address) dieuts.get(k);
                     latLng = new LatLng(address1.getLatitude(), address1.getLongitude());
                     String address1Text = String.format("%s,%s", address1.getMaxAddressLineIndex() > 0 ? address1.getAddressLine(0) : "", address1.getCountryName());

                     markerOpt = new MarkerOptions();
                    markerOpt.position(latLng);

                markerOpt.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));

                     markerOpt.title(address1Text);
                    maps.addMarker(markerOpt);

                    if (k == 0)
                        maps.animateCamera(CameraUpdateFactory.newLatLng(latLng));

                        if(markerPoints.size()==2) {

                            //lhpsh URL sto Google Directions API
                            String url1 = getDirectionsUrl(markerPoints.get(1));

                            DownloadTask1 downloadTask1 = new DownloadTask1();

                            //xekinhste thn lhpsh dedomenwn json apo to Google Directions API
                            downloadTask1.execute(url1);
                        }
                 }
                // } catch (NullPointerException e) {
                  //   e.printStackTrace();
                 //}
             }
        }

网站上存在的答案无法解决问题。

1 个答案:

答案 0 :(得分:1)

而不是声明List<Address>addresses=null;执行此操作

List<Address>addresses = new ArrayList<>();

List<Address> dieuts = null;

相同
List<Address> dieuts = new ArrayList<>();

这应该可以解决问题