gpt4 book ai didi

java - setTypeface 空指针异常

转载 作者:行者123 更新时间:2023-12-01 12:04:15 25 4
gpt4 key购买 nike

我又遇到了另一个问题,这次更令人困惑:

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 上搜索答案也是如此)您知道导致此错误的原因吗?可以帮我指出吗?

最佳答案

您忘记为您的 Activity 提供布局。你必须打电话

setContentView(R.layout.activity)

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View).

您可以阅读更多here .

关于java - setTypeface 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27755562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com