當我想要在列表視圖上顯示數據時,我變空了。 日誌告訴大家,問題是這一行:NullException當我在TextView上顯示一個數字
holder.temperatureLabel.setText(day.getTemperatureMax()+ "");
末是這樣的錯誤:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.hay.stormy.adapters.DayAdapter.getView(DayAdapter.java:64)
我也試過只給一個常數或常量字符串,其仍與崩潰同樣的錯誤,但如果我不跑這條線,我感到我需要的數據,怪異的人,其餘的應用...
一些代碼ü理解:
我的ViewHolder和getView:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// brand new
convertView = LayoutInflater.from(mContext).inflate(R.layout.daily_list_item, null);
holder = new ViewHolder();
holder.iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
holder.temperatureLabel = (TextView) convertView.findViewById(R.id.temperatureLabel);
holder.dayLabel = (TextView) convertView.findViewById(R.id.dayNameLabel);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Day day = mDays[position];
holder.iconImageView.setImageResource(day.getIconId());
holder.temperatureLabel.setText(day.getTemperatureMax()+ "");
if (position == 0) {
holder.dayLabel.setText("Today");
}
else {
holder.dayLabel.setText(day.getDayOfTheWeek());
}
return convertView;
}
private static class ViewHolder {
ImageView iconImageView; // public by default
TextView temperatureLabel;
TextView dayLabel;
}
和我的getter和setter在天類有問題的參數:
public int getTemperatureMax() {
return (int)Math.round(mTemperatureMax);
}
public void setTemperatureMax(double temperatureMax) {
mTemperatureMax = temperatureMax;
}
full logcat?另外,'DayAdapter'行#64中發生了什麼? – thepoosh