2014-12-15 2 views
0

У меня есть следующий код, чтобы использовать weatherlib для получения погодных условий в android с помощью студии android.Невозможно выполнить активность в android for weatherlib

Моя активность:

import android.app.Activity; 
    import android.content.Context; 
    import android.os.Bundle; 
    import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Toast; 

import com.survivingwithandroid.weather.lib.WeatherClient; 
import com.survivingwithandroid.weather.lib.WeatherClientDefault; 
import com.survivingwithandroid.weather.lib.WeatherConfig; 
import com.survivingwithandroid.weather.lib.exception.LocationProviderNotFoundException; 
import com.survivingwithandroid.weather.lib.exception.WeatherLibException; 
import com.survivingwithandroid.weather.lib.model.CurrentWeather; 
import com.survivingwithandroid.weather.lib.provider.IWeatherProvider; 
import com.survivingwithandroid.weather.lib.provider.WeatherProviderFactory; 
import com.survivingwithandroid.weather.lib.provider.openweathermap.OpenweathermapProviderType; 

import java.util.List; 


public class MainActivity extends Activity { 

    private Context ctx; 



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

     WeatherClient client = WeatherClientDefault.getInstance(); 
     client.init(ctx); 

     WeatherConfig config = new WeatherConfig(); 
     config.unitSystem = WeatherConfig.UNIT_SYSTEM.M; 
     config.lang = "en"; // If you want to use english 
     config.maxResult = 5; // Max number of cities retrieved 
     config.numDays = 6; // Max num of days in the forecast 

     client.updateWeatherConfig(config); 


     IWeatherProvider provider = null; 
     try { 
     //provider = WeatherProviderFactory.createProvider(new YahooProviderType(), config); 
      provider = WeatherProviderFactory.createProvider(new OpenweathermapProviderType(), config); 
     //provider = WeatherProviderFactory.createProvider(new WeatherUndergroundProviderType(), config); 
      client.setProvider(provider); 
    } 
    catch (Throwable t) { 
     // There's a problem 
    } 

     try { 
      client.searchCityByLocation(WeatherClient.createDefaultCriteria(), new WeatherClient.CityEventListener() { 

       @Override 
       public void onCityListRetrieved(List<City> cityList) { 
       // Here your logic when the data is available 

      } 

       @Override 
       public void onWeatherError(WeatherLibException wle) { 

      } 

       @Override 
        public void onConnectionError(Throwable t) { 

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

     client.getCurrentCondition("29222451", new WeatherClient.WeatherEventListener() { 
      @Override 
      public void onWeatherRetrieved(CurrentWeather weather) { 
       // Here we can use the weather information to upadte the view 
       String text = weather.currentCondition.getCondition(); 
       String text2 = weather.currentCondition.getDescr(); 
       float temp = weather.temperature.getTemp(); 

       Toast.makeText(getApplicationContext(), "Condition :" + text + " Description " + text2 + " Temperature " + temp, 
        Toast.LENGTH_LONG).show(); 
      } 

      @Override 
      public void onWeatherError(WeatherLibException t) { 

      } 

      @Override 
      public void onConnectionError(Throwable t) { 

      } 
     }); 

    } 

Я получаю следующее сообщение об ошибке для этого:

java.lang.RuntimeException: Unable to start activity  ComponentInfo{MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void com.survivingwithandroid.weather.lib.provider.IWeatherProvider.setConfig(com.survivingwithandroid.weather.lib.WeatherConfig)' on a null object reference 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
     at android.app.ActivityThread.access$800(ActivityThread.java:144) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5221) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.survivingwithandroid.weather.lib.provider.IWeatherProvider.setConfig(com.survivingwithandroid.weather.lib.WeatherConfig)' on a null object reference 
     at com.survivingwithandroid.weather.lib.WeatherClientDefault.updateWeatherConfig(WeatherClientDefault.java:104) 
     i.MainActivity.onCreate(MainActivity.java:41) 
     at android.app.Activity.performCreate(Activity.java:5933) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
     at android.app.ActivityThread.access$800(ActivityThread.java:144) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5221) 

ответ

0

Я думаю, вы не импортировать проект правильно или штрафной правильное разрешение на проект.

1

client.updateWeatherConfig(config); должна вызываться после инициализации поставщика, как:

WeatherClient client = WeatherClientDefault.getInstance(); 
    client.init(ctx); 

    WeatherConfig config = new WeatherConfig(); 
    config.unitSystem = WeatherConfig.UNIT_SYSTEM.M; 
    config.lang = "en"; // If you want to use english 
    config.maxResult = 5; // Max number of cities retrieved 
    config.numDays = 6; // Max num of days in the forecast 


    IWeatherProvider provider = null; 
    try { 
     //provider = WeatherProviderFactory.createProvider(new YahooProviderType(), config); 
     provider = WeatherProviderFactory.createProvider(new OpenweathermapProviderType(), config); 
     //provider = WeatherProviderFactory.createProvider(new WeatherUndergroundProviderType(), config); 
     client.setProvider(provider); 

    } 
    catch (Throwable t) { 
     // There's a problem 
    } 

    client.updateWeatherConfig(config);