【问题标题】:ImageView throwing NullReferenceException on Inflated LinearLayoutImageView 在 Inflated LinearLayout 上抛出 NullReferenceException
【发布时间】:2011-06-01 03:44:39
【问题描述】:

在使用 LayoutInflater 膨胀父布局后,ImageView 上的 NullReferenceExcpetion 出现了一些问题。正如您在下面的布局 XML 中看到的,我有两个 TextView 和一个 ImageView。我可以很好地引用两个 TextView,但不能引用 ImageView。

当我深入了解膨胀布局的子视图的属性时,我看到两个 TextView 的 mID 属性都是正确的,但是 ImageView 的 mID 是-1

有人知道为什么只有 ImageView 出现为 NULL 吗?我确定这很愚蠢,但我就是想不通。我还重新创建了 Layout 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");

【问题讨论】:

  • 我打算建议你清理并重建你的项目。然后我注意到你说你“清理了我的解决方案”。如果这并不意味着同样的事情,那么请尝试清理并重建您的项目。
  • 是的,我的意思是项目。我已经更新了问题,感谢您的提醒。

标签: android imageview nullreferenceexception layout-inflater


【解决方案1】:

我不知何故让这个工作。我正在回答我自己的问题,因为该问题已通过其他方式解决,然后提供了解决方案,但是,Jaydeep 和 CapDroid 的两个答案都可以正常工作以膨胀视图。

在 Inflating 我的视图中尝试了多种不同的方法后,我第三次重新创建了 Layout XML 文件,更改了 ID 并重新清理了项目。在这个过程中,我的问题不知何故消失了,所以我相信这是我的设置或系统的问题。

感谢所有提供帮助和指导的人!

【讨论】:

    【解决方案2】:

    试试这个代码:

    String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li = (LayoutInflater)ctx.getSystemService(inflater);
        v = li.inflate(R.layout.icon, null);
    

    【讨论】:

      【解决方案3】:

      试试这个...

      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));
      

      【讨论】:

      • 我在尝试两个代码示例时得到了相同的结果。 ImageView 仍然为空。
      猜你喜欢
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多