无法在android中为weatherlib执行活动

时间:2014-12-15 09:58:29

标签: android android-studio yahoo-api weather-api

我有以下代码使用weatherlib在android中使用android studio获取天气状况。

我的活动:

 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)

2 个答案:

答案 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);

答案 1 :(得分:0)

我猜您没有正确导入项目,或者您没有获得项目的正确权限。