2013-01-17 67 views
0

我一直在寻找所有的stackoverflow找到解决方案,但我还没有找到它。所以,现在我想说如果发现重复,我很抱歉。现在,我已经说,让我们得到一些代码和问题:Android:ImageView将不会被加载

我想一个ImageView加载到我的活动,但我不断收到一个不错的蓝色调试日志我的logcat说"imageview == null"

if (imageview == null) { 
    Log.d(TAG, "imageview == null"); 
} else { 
    imageview.setImageBitmap(bm); }` 

它只是检查我的ImageView是否为空(并且如果不为空则将图像置入它中)。我尝试加载它在onCreate方法:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.confirmpicture); 
    String str = getIntent().getStringExtra("GalleryImage"); 
    Log.i(TAG, "Got " + str + " from the intent"); 
    Bitmap bm = BitmapFactory.decodeFile(str); 
    imageview = (ImageView) findViewById(R.id.galleryPicture); 
    if (bm != null) { 
     if (imageview == null) { 
      Log.d(TAG, "imageview == null"); 
     } else { 
      imageview.setImageBitmap(bm); 
     } 
    } else { 
     Log.d(TAG, "Bitmap not decoded"); 
    } 

我的头一直问我:“你在XML文件中声明ImageView的,你这个傻瓜?”我继续检查其包含

<ImageView 
    android:name="@+id/galleryPicture" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:layout_below="@id/confirmText" 
    android:contentDescription="description comes here...." /> 

我靶向API 8 XML文件(Android 2.2的),我一直在试图解决关于现在加载ImageView几天这个问题。现在修复

+1

你确定你在这里加载正确的布局(的setContentView(R.layout.confirmpicture );)? – Nermeen

+0

请检查str对象或getIntent()。getStringExtra(“GalleryImage”)可能是其中一个给你null – Pratik

+0

是Nunu,我正在加载正确的布局文件。 – Zuenonentu

回答

2

您应该在xml文件中写入android:id="@+id/galleryPicture",而不是android:name="@+id/galleryPicture"。在您创建的布局文件中没有id galleryPicture的视图。

+0

这是一个好的捕获 –

+0

谢谢,我可以把我的现在回头发...... – Zuenonentu

0

尝试设置的android:id="@+id/galleryPicture"代替android:name="@+id/galleryPicture",也可以设置在图像中的ImageView如下:

<ImageView 
android:id="@+id/galleryPicture" <------ set proper id. 
android:layout_width="match_parent" 
android:layout_height="100dp" 
android:layout_below="@id/confirmText" 
android:background="@drawable/someimage" <------Set background 
android:contentDescription="description comes here...." /> 
+0

感谢您的回答,您对此也绝对正确.... – Zuenonentu