无法在Android的执行活动weatherlib [英] Unable to execute activity in android for weatherlib
本文介绍了无法在Android的执行活动weatherlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下code使用weatherlib采用了android工作室,获取天气情况的机器人。
我的活动:
进口android.app.Activity;
进口android.content.Context;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.Toast;进口com.survivingwithandroid.weather.lib.WeatherClient;
进口com.survivingwithandroid.weather.lib.WeatherClientDefault;
进口com.survivingwithandroid.weather.lib.WeatherConfig;
进口com.survivingwithandroid.weather.lib.exception.LocationProviderNotFoundException;
进口com.survivingwithandroid.weather.lib.exception.WeatherLibException;
进口com.survivingwithandroid.weather.lib.model.CurrentWeather;
进口com.survivingwithandroid.weather.lib.provider.IWeatherProvider;
进口com.survivingwithandroid.weather.lib.provider.WeatherProviderFactory;
进口com.survivingwithandroid.weather.lib.provider.openweathermap.OpenweathermapProviderType;进口的java.util.List;
公共类MainActivity延伸活动{ 私人上下文CTX; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); WeatherClient客户= WeatherClientDefault.getInstance();
client.init(CTX); WeatherConfig配置=新WeatherConfig();
config.unitSystem = WeatherConfig.UNIT_SYSTEM.M;
config.lang =EN; //如果你想使用英语
config.maxResult = 5; //城市的最大数量检索
config.numDays = 6;在预测天//最大NUM client.updateWeatherConfig(配置);
IWeatherProvider提供商= NULL;
尝试{
//提供商= WeatherProviderFactory.createProvider(新YahooProviderType(),配置);
供应商= WeatherProviderFactory.createProvider(新OpenweathermapProviderType(),配置);
//提供商= WeatherProviderFactory.createProvider(新WeatherUndergroundProviderType(),配置);
client.setProvider(供应商);
}
捕获(的Throwable t)的{
//有一个问题
} 尝试{
client.searchCityByLocation(WeatherClient.createDefaultCriteria(),新WeatherClient.CityEventListener(){ @覆盖
公共无效onCityListRetrieved(列表<城市> cityList){
//这里你的逻辑,当数据可用 } @覆盖
公共无效onWeatherError(WeatherLibException WLE){ } @覆盖
公共无效onConnectionError(的Throwable t)的{ }
});
}赶上(LocationProviderNotFoundException E){
e.printStackTrace();
} client.getCurrentCondition(29222451,新WeatherClient.WeatherEventListener(){
@覆盖
公共无效onWeatherRetrieved(CurrentWeather天气){
//这里我们可以使用天气信息UPADTE视图
字符串文本= weather.currentCondition.getCondition();
字符串文本2 = weather.currentCondition.getDescr();
浮TEMP = weather.temperature.getTemp(); Toast.makeText(getApplicationContext(),条件:+文字+说明+文本2 +温度+温度,
Toast.LENGTH_LONG).show();
} @覆盖
公共无效onWeatherError(WeatherLibException T){ } @覆盖
公共无效onConnectionError(的Throwable t)的{ }
}); }
我收到以下错误这样的:
了java.lang.RuntimeException:无法启动活动ComponentInfo {} MainActivity:显示java.lang.NullPointerException:尝试调用接口方法无效com.survivingwithandroid.weather.lib.provider.IWeatherProvider.setConfig(com.survivingwithandroid.weather.lib.WeatherConfig)'在一个空对象引用
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
在android.app.ActivityThread.access $ 800(ActivityThread.java:144)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:135)
在android.app.ActivityThread.main(ActivityThread.java:5221)
在java.lang.reflect.Method.invoke(本机方法)
在java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
显示java.lang.NullPointerException:引起尝试调用接口方法无效com.survivingwithandroid.weather.lib.provider.IWeatherProvider.setConfig(com.survivingwithandroid.weather.lib.WeatherConfig)'在一个空对象引用
在com.survivingwithandroid.weather.lib.WeatherClientDefault.updateWeatherConfig(WeatherClientDefault.java:104)
i.MainActivity.onCreate(MainActivity.java:41)
在android.app.Activity.performCreate(Activity.java:5933)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
在android.app.ActivityThread.access $ 800(ActivityThread.java:144)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:135)
在android.app.ActivityThread.main(ActivityThread.java:5221)
解决方案
我猜你没有正确导入项目或者你没有正确的许可项目。
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)
解决方案
I guess you didn't import the project correctly or you didnt the right permission to the project.
这篇关于无法在Android的执行活动weatherlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文