在使用location时android中的RunTimeException

时间:2013-11-23 20:51:21

标签: android

我正在使用openweather API创建android应用程序,我想获取当前位置并将其解析为URL,

我写了这段代码,但代码崩溃了:

public class MainActivity extends FragmentActivity implements LocationListener {


    private TextView cityText;
    private TextView condDescr;
    private TextView temp;
    private TextView press;
    private TextView windSpeed;
    private TextView windDeg;
    private TextView unitTemp;

    private TextView hum;
    private ImageView imgView;
    public double lat=0.0;
    public double longi=0.0;
    private static String forecastDaysNum = "3";
    private ViewPager pager;

     LocationManager manager;
        LocationProvider provider;
        Criteria criteria;
        private Context context=null;
        public String city="";

////////////////////////////
public String getAddress(double latitude, double longitude){
if (Geocoder.isPresent()) {

Geocoder geocoder = new Geocoder(context, Locale.getDefault());

List<Address> addresses = null;
try {
    addresses = geocoder.getFromLocation(lat,longi,1);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

if(addresses != null && addresses.size() > 0) {
Address first = addresses.get(0);
StringBuilder sb = new StringBuilder();

if(first.getMaxAddressLineIndex() > 0){
sb.append(first.getAddressLine(0) + ", ");
}

sb.append(first.getLocality() + ", ");
sb.append(first.getCountryName());

//return sb.toString();
city=sb.toString();
//return city;
//return city;
}}
return city;}
//////////////////////////

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //String city = "London, UK";

        manager=(LocationManager) getSystemService(context.LOCATION_SERVICE);
         criteria=new Criteria();
        String provider=manager.getBestProvider(criteria, false);
        android.location.Location loc=manager.getLastKnownLocation(provider);

        //double
    //  lat=loc.getLatitude();
    //  double
    //  longi=loc.getLongitude();

        city=getAddress(lat, longi);

        String lang = "en";

        cityText = (TextView) findViewById(R.id.cityText);
        temp = (TextView) findViewById(R.id.temp);
        unitTemp = (TextView) findViewById(R.id.unittemp);
        unitTemp.setText("°C");
        condDescr = (TextView) findViewById(R.id.skydesc);

        pager = (ViewPager) findViewById(R.id.pager);
        imgView = (ImageView) findViewById(R.id.condIcon);
        /*
        condDescr = (TextView) findViewById(R.id.condDescr);

        hum = (TextView) findViewById(R.id.hum);
        press = (TextView) findViewById(R.id.press);
        windSpeed = (TextView) findViewById(R.id.windSpeed);
        windDeg = (TextView) findViewById(R.id.windDeg);

        */





        JSONWeatherTask task = new JSONWeatherTask();
        task.execute(new String[]{city,lang});

        JSONForecastWeatherTask task1 = new JSONForecastWeatherTask();
        task1.execute(new String[]{city,lang, forecastDaysNum});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    private class JSONWeatherTask extends AsyncTask<String, Void, Weather> {

        @Override
        protected Weather doInBackground(String... params) {
            Weather weather = new Weather();
            String data = ( (new WeatherHttpClient()).getWeatherData(params[0], params[1]));

            try {
                weather = JSONWeatherParser.getWeather(data);
                System.out.println("Weather ["+weather+"]");
                // Let's retrieve the icon
                weather.iconData = ( (new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));

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

    }


    @Override
    protected void onPostExecute(Weather weather) {         
            super.onPostExecute(weather);

            if (weather.iconData != null && weather.iconData.length > 0) {
                Bitmap img = BitmapFactory.decodeByteArray(weather.iconData, 0, weather.iconData.length); 
                imgView.setImageBitmap(img);
            }


            cityText.setText(weather.location.getCity() + "," + weather.location.getCountry());
            temp.setText("" + Math.round((weather.temperature.getTemp() - 275.15)));
            condDescr.setText(weather.currentCondition.getCondition() + "(" + weather.currentCondition.getDescr() + ")");
            /*

            temp.setText("" + Math.round((weather.temperature.getTemp() - 275.15)) + "°C");
            hum.setText("" + weather.currentCondition.getHumidity() + "%");
            press.setText("" + weather.currentCondition.getPressure() + " hPa");
            windSpeed.setText("" + weather.wind.getSpeed() + " mps");
            windDeg.setText("" + weather.wind.getDeg() + "°");
            */  
        }



  }





    private class JSONForecastWeatherTask extends AsyncTask<String, Void, WeatherForecast> {

        @Override
        protected WeatherForecast doInBackground(String... params) {

            String data = ( (new WeatherHttpClient()).getForecastWeatherData(params[0], params[1], params[2]));
            WeatherForecast forecast = new WeatherForecast();
            try {
                forecast = JSONWeatherParser.getForecastWeather(data);
                System.out.println("Weather ["+forecast+"]");
                // Let's retrieve the icon
                //weather.iconData = ( (new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));

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

    }




    @Override
        protected void onPostExecute(WeatherForecast forecastWeather) {         
            super.onPostExecute(forecastWeather);

            DailyForecastPageAdapter adapter = new DailyForecastPageAdapter(Integer.parseInt(forecastDaysNum), getSupportFragmentManager(), forecastWeather);

            pager.setAdapter(adapter);
        }



  }





    @Override
    public void onLocationChanged(android.location.Location location) {
        // TODO Auto-generated method stub
        lat=location.getLatitude();
        longi=location.getLongitude();
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }





}

我收到了RunTimeException。

logCat表示Invalid Provider:Null

0 个答案:

没有答案