在网上看了个例子,依着葫芦画瓢,先不谈与屏幕等宽,随便设了个高和宽,不知为何报空指针异常
public class Test extends Activity {
  private Bitmap bmImg;
    private ImageView imView;
    private static final String tag = "Image Top";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                
        bmImg = BitmapFactory.decodeResource(getResources(), R.drawable.top);
        imView.setImageDrawable(resizeImage(bmImg, 200, 200));    }
    
        
    public static Drawable resizeImage(Bitmap bitmap, int w, int h) {
        Bitmap BitmapOrg = bitmap;        int width = BitmapOrg.getWidth();
        int height = BitmapOrg.getHeight();
        int newWidth = w;
        int newHeight = h;
        
        Log.v(tag,String.valueOf(width));
        Log.v(tag,String.valueOf(height));
        
        Log.v(tag,String.valueOf(newWidth));
        Log.v(tag,String.valueOf(newHeight));        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
                        height, matrix, true);        return new BitmapDrawable(resizedBitmap);       }    
}
我确定图片是必然存在的,xml是默认的没改,不知错在哪了啊

解决方案 »

  1.   

    xml中加了TextView,如下,依然是空指针异常
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <ImageView android:id="@+id/logo"
         android:src="@drawable/top"
         android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        />
        
    </LinearLayout>
      

  2.   

    bmImg = BitmapFactory.decodeResource(getResources(), R.drawable.top);
    将R.drawable.top改为R.id.logo试试
      

  3.   

    出来了,原来是掉了一句
    imView = (ImageView)findViewById(R.id.logo);