我又遇到了另一个问题,这次更令人困惑:
public class MainActivity extends Activity {
TextView cityData;
TextView updatedData;
TextView detailsData;
TextView currentTemperatureData;
TextView weatherIcon;
Typeface weatherIcons;
Handler handleWeather;
public MainActivity() {
handleWeather = new Handler();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cityData = (TextView) findViewById(R.id.city_field);
updatedData = (TextView) findViewById(R.id.updated_field);
detailsData = (TextView) findViewById(R.id.details_field);
currentTemperatureData = (TextView) findViewById(R.id.current_temperature_field);
weatherIcon = (TextView) findViewById(R.id.weather_icon);
weatherIcons = Typeface.createFromAsset(getAssets(), "weather.ttf"); //fonts found from https://github.com/erikflowers/weather-icons
weatherIcon.setTypeface(weatherIcons);
在这里,在这段代码中,我的错误在于weatherIcon.setTypeface(weatherIcons);
,它接收到NullPointerException,即使我在长时间搜索后切换了其原点我找不到的最后两行的位置。 (同样在stackoverflow上搜索答案)
你知道是什么导致了这个错误吗?请你帮忙指出来吗?
答案 0 :(得分:1)
您忘记为您的活动提供布局。你必须致电
setContentView(R.layout.activity)
活动是用户可以做的一件重点事。几乎 所有活动都与用户交互,因此Activity类占用 为您创建一个可以放置UI的窗口 的的setContentView(查看)强>
您可以阅读更多here。
答案 1 :(得分:0)
尝试以下修改后的代码:
public class MainActivity extends Activity {
TextView cityData;
TextView updatedData;
TextView detailsData;
TextView currentTemperatureData;
TextView weatherIcon;
Typeface weatherIcons;
Handler handleWeather;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xml_file);
cityData = (TextView) findViewById(R.id.city_field);
updatedData = (TextView) findViewById(R.id.updated_field);
detailsData = (TextView) findViewById(R.id.details_field);
currentTemperatureData = (TextView) findViewById(R.id.current_temperature_field);
weatherIcon = (TextView) findViewById(R.id.weather_icon);
weatherIcons = Typeface.createFromAsset(getAssets(), "weather.ttf"); //fonts found from https://github.com/erikflowers/weather-icons
weatherIcon.setTypeface(weatherIcons);