尝试使用ImageView时出现NullPointerException

时间:2014-06-13 11:40:24

标签: reflection nullpointerexception imageview

我从另一个网站抓取以下代码 - 我找不到它的问题,但它一直给我一个nullpointerexception。我认为这个问题在setContentView之前的第一部分是正确的,但包括整个事情以防万一。在此先感谢您的帮助!

public class TileSet extends Activity {

Bitmap originalImage = BitmapFactory.decodeResource(getResources(),
        R.drawable.announcementbc);

ImageView imageView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub



    super.onCreate(savedInstanceState);



    //Create an Image view and add our bitmap with reflection to it

    imageView.setImageBitmap(getRefelection(originalImage));

    //imageView.setImageBitmap(image);

    //Add the image to a linear layout and display it
    LinearLayout linLayout = new LinearLayout(this);        
    linLayout.addView(imageView, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    // set LinearLayout as ContentView

    setContentView(linLayout);

}

    // public static Bitmap getRefelection(Bitmap image)

 public static Bitmap getRefelection(Bitmap image)
  {
     //The gap we want between the reflection and the original image
       final int reflectionGap = 4;

       //Get you bit map from drawable folder
       Bitmap originalImage = image;


       int width = originalImage.getWidth();
       int height = originalImage.getHeight();


       //This will not scale but will flip on the Y axis
       Matrix matrix = new Matrix();
       matrix.preScale(1, -1);

       //Create a Bitmap with the flip matix applied to it.
       //We only want the bottom half of the image
       Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/2, width, height/2, matrix, false);


       //Create a new bitmap with same width but taller to fit reflection
       Bitmap bitmapWithReflection = Bitmap.createBitmap(width
       , (height + height/2), Config.ARGB_8888);

      //Create a new Canvas with the bitmap that's big enough for
      //the image plus gap plus reflection
      Canvas canvas = new Canvas(bitmapWithReflection);
      //Draw in the original image
      canvas.drawBitmap(originalImage, 0, 0, null);
      //Draw in the gap
      Paint deafaultPaint = new Paint();
      canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
      //Draw in the reflection
      canvas.drawBitmap(reflectionImage,0, height + reflectionGap, null);

      //Create a shader that is a linear gradient that covers the reflection
      Paint paint = new Paint();
      LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
      bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff,
      TileMode.CLAMP);
      //Set the paint to use this shader (linear gradient)
      paint.setShader(shader);
      //Set the Transfer mode to be porter duff and destination in
      paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
      //Draw a rectangle using the paint with our linear gradient
      canvas.drawRect(0, height, width,
      bitmapWithReflection.getHeight() + reflectionGap, paint);
      reflectionImage.recycle();
      return bitmapWithReflection;
   }

}

2 个答案:

答案 0 :(得分:0)

    imageView.setImageBitmap(getRefelection(originalImage));

这是它崩溃的行,原因是imageViewnull

当您创建一个成员变量(例如您列出的变量)时,您需要为其指定一个对象,或者将其设置为已创建的对象。

在你的情况下,我猜你想把它设置为你的布局已经创建的那个。您的布局XML应该声明一些ImageView,但是为了在Java代码中获取它,您需要调用findViewById()

如果您的XML声明为id

<ImageView
    android:id="@+id/my_image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

然后,您需要先写下以下内容才能拨打setImageBitmap()

imageView = (ImageView) findViewById(R.id.my_image_view);

此外,无法拨打findViewById ,直到之后拨打setContentView(),因为这是实际创建您的电话ImageView

虽然进一步查看代码,但您可能不需要执行反射位,甚至是setImageBitmap()调用,因为您可以直接在XML中指定drawable(除非您想要在程序运行时更改它:

<ImageView
    android:id="@+id/my_image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/announcementbc"
    />

通过添加源代码行并将其指向drawable,setContentView调用将自动在ImageView上设置正确的位图。

答案 1 :(得分:0)

以下是根据上述评论

的最新失败代码

公共类Tile2扩展了Activity {

// Bitmap originalImage = BitmapFactory.decodeResource(getResources(), R.drawable.announcementbc);

ImageView imageView;
Bitmap originalImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView (R.layout.tile);




    imageView = (ImageView) findViewById(R.id.ivTile);
    originalImage = BitmapFactory.decodeResource(getResources(), R.drawable.announcementbc);
    // ImageView imageView = new ImageView(this);
    imageView.setImageBitmap(getReflection(originalImage));

    //Add the image to a linear layout and display it
    LinearLayout linLayout = new LinearLayout(this);
    linLayout.addView(imageView, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

     // set LinearLayout as ContentView
     setContentView(linLayout);





}

public static Bitmap getReflection(Bitmap image)
   {
     //The gap we want between the reflection and the original image
       final int reflectionGap = 4;

       //Get you bit map from drawable folder
       Bitmap originalImage = image;


       int width = originalImage.getWidth();
       int height = originalImage.getHeight();


       //This will not scale but will flip on the Y axis
       Matrix matrix = new Matrix();
       matrix.preScale(1, -1);

       //Create a Bitmap with the flip matix applied to it.
       //We only want the bottom half of the image
       Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/2, width, height/2, matrix, false);


       //Create a new bitmap with same width but taller to fit reflection
       Bitmap bitmapWithReflection = Bitmap.createBitmap(width
         , (height + height/2), Config.ARGB_8888);

      //Create a new Canvas with the bitmap that's big enough for
      //the image plus gap plus reflection
      Canvas canvas = new Canvas(bitmapWithReflection);
      //Draw in the original image
      canvas.drawBitmap(originalImage, 0, 0, null);
      //Draw in the gap
      Paint deafaultPaint = new Paint();
      canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
      //Draw in the reflection
      canvas.drawBitmap(reflectionImage,0, height + reflectionGap, null);

      //Create a shader that is a linear gradient that covers the reflection
      Paint paint = new Paint();
      LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
        bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff,
        TileMode.CLAMP);
      //Set the paint to use this shader (linear gradient)
      paint.setShader(shader);
      //Set the Transfer mode to be porter duff and destination in
      paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
      //Draw a rectangle using the paint with our linear gradient
      canvas.drawRect(0, height, width,
        bitmapWithReflection.getHeight() + reflectionGap, paint);
      reflectionImage.recycle();
      return bitmapWithReflection;
   }

}