我在使用LayoutInflater来膨胀父级布局后,在ImageView上的NullReferenceExcpetion时出现了一些问题。正如你可以在下面的布局XML中看到的,我有两个TextView和一个ImageView。我可以很好地引用两个TextView,但不是ImageView。ImageView在膨胀的LinearLayout上抛出NullReferenceException
当我钻到到的胖布局的子视图属性,我看到,无论是TextViews的中期财产是正确的,但是的ImageView的中期是-1
会有人有任何想法为什么只有ImageView会成为NULL?我相信这是愚蠢的,但我无法弄清楚。我也重新创建了布局XML文件,清理了我的项目等。
在此先感谢!
布局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout android:layout_width="120px"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/weather_forecast_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textStyle="bold"/>
<ImageView android:id="@+id/weather_forecast_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<TextView android:id="@+id/weather_forecast_temps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
代码:
mForecastItem = (LinearLayout)View.inflate(WeatherLookup.this, R.layout.weather_forecast_item, null);
((ImageView)mForecastItem.findViewById(R.id.weather_forecast_icon))
.setImageDrawable(getResources().getDrawable(R.drawable.weather_sunny));
((TextView)mForecastItem.findViewById(R.id.weather_forecast_day))
.setText(forecast.DayOfWeek);
((TextView)mForecastItem.findViewById(R.id.weather_forecast_temps))
.setText(forecast.High + "\u2109/" + forecast.Low + "\u2109");
我打算建议您清理并重建您的项目。然后我发现你说你“清理了我的解决方案”。如果这并不意味着同样的事情,那么请尝试清理并重建您的项目。 – 2011-06-01 03:48:27
是的,我的意思是项目。我已经更新了这个问题,谢谢你的支持。 – hooked82 2011-06-01 03:53:33