Unable to execute activity in android for weatherlib

走远了吗. 提交于 2019-12-11 16:16:41

问题


I have following code to use weatherlib to get weather condition in android using android studio.

My activity:

 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) {

          }
       });

   }

I get the following error for this:

   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)

回答1:


client.updateWeatherConfig(config); must be called after initializing provider as:

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



回答2:


I guess you didn't import the project correctly or you didnt the right permission to the project.



来源:https://stackoverflow.com/questions/27481420/unable-to-execute-activity-in-android-for-weatherlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!