Reputation: 6376
I'm having some problems with a NullReferenceExcpetion on an ImageView after I use a LayoutInflater to inflate the parent layout. As you can see in the Layout XML below, I have two TextView's and one ImageView. I can reference both of the TextViews just fine, but not the ImageView.
When I drilldown into the properties of the inflated layout's child views, I see that the mID property of both the TextViews are correct, however the mID of the ImageView is -1
Would anybody have any idea why only the ImageView is coming up as NULL? I'm sure it's something stupid, but I just can't figure it out. I've also recreated the Layout XML file, cleaned my project, etc.
Thanks in advance!!
Layout 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>
Code:
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");
Upvotes: 1
Views: 643
Reputation: 6376
I somehow got this to work. I'm answering my own question due to the fact that the issue was resolved by other ways that then provided solutions, however, both answers by Jaydeep and CapDroid do work properly for inflating a view.
After trying multiple different ways in Inflating my view, I recreated the Layout XML file for a third time, changed the IDs and re-cleaned the Project. Somehow in this process, my issue went away, so I'm led to believe that it was an issue with either my setup or system.
Thanks to all who provided help and guidance!
Upvotes: 1
Reputation: 33238
try this...
LayoutInflater inflater;
inflater = activity.getLayoutInflater();
mForecastItem = (LinearLayout)inflater.inflate(
R.layout.weather_forecast_item, null);
((ImageView)mForecastItem.findViewById(R.id.weather_forecast_icon))
.setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable.weather_sunny));
Upvotes: 0
Reputation: 5985
Try this code:
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)ctx.getSystemService(inflater);
v = li.inflate(R.layout.icon, null);
Upvotes: 0