版本2.0
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <ImageView android:id="@+id/UIimg1" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>java文件
package slash.UISample;import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class ImageViewSample extends Activity{
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImageView iv1;
        iv1=(ImageView)findViewById(R.id.UIimg1);
        iv1.setImageResource(R.drawable.slash);
        setContentView(iv1);
    }
}slash.png文件也放在了res下三个drawable文件夹里面了
运行的时候提示错误。。以前是好的,不知道为啥呢

解决方案 »

  1.   

    得先setContentView(R.layout.你的xml文件名);才能findViewById(R.id.UIimg1);
      

  2.   

    可以运行起来,但是提示
    The application UISample(process slash.UISample) has stopped unexpectedly.please try it again
      

  3.   

    哦,是错了
    ImageView iv1;
      iv1=(ImageView)findViewById(R.id.UIimg1);//调用findViewById的前提是已经调用setContentView,也就是说界面布局都创建完毕了
      iv1.setImageResource(R.drawable.slash);
      setContentView(iv1);
    所以要这样:
    setContentView(R.id.layout);//layout.xml中是界面的布局就是你的xml文件
      ImageView iv1=(ImageView)findViewById(R.id.UIimg1);
      iv1.setImageResource(R.drawable.slash);
      
      

  4.   

    不知道是不是我的Eclipse出了问题
    ImageView iv=new ImageView(this);
    iv.setImageResource(R.drawable.icon);
    setContentView(iv);
    这个以前代码可以显示的,现在也显示不了
      

  5.   

    1.5可以正常显示
     ImageView iv=new ImageView(this);
            iv.setImageResource(R.drawable.icon);
            setContentView(iv);
    2.0就不能显示了么。。