0

I am trying to setwallpaper from my class which extends view superclass , i am trying to convert view into the bitmap but i am getting an error (NullPointerException).

case R.id.wallpaper: // This is an event of my button

        View view = new CustomWallpaper(this);


        b = convertToBitmap(view);

        WallpaperManager myWallpaperManager
         = WallpaperManager.getInstance(getApplicationContext());

        try {
            myWallpaperManager.setBitmap(b);
            new CustomToast(context, "Wallpaper has been set").show();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        break;

and this is my method

private Bitmap convertToBitmap(View view) {
    // TODO Auto-generated method stub

    Bitmap viewCapture = null;

    view.setDrawingCacheEnabled(true);

    viewCapture = Bitmap.createBitmap(view.getDrawingCache());

    view.setDrawingCacheEnabled(false);

    return viewCapture;
}

And this is my class which extends view

public class CustomWallpaper extends View {

public CustomWallpaper(Context context) {
    super(context);
    // TODO Auto-generated constructor stub

    setBackgroundColor(Color.BLACK);

    LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);

    setLayoutParams(params);
}

@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    Paint paint = new Paint();

    paint.setColor(Color.RED);

    canvas.drawCircle(50, 50, 30, paint);

}

}

Thank You

Dardan
  • 531
  • 7
  • 15
  • Post your logcat at where you got null pointer exception.. – Piyush Dec 03 '13 at 12:20
  • After added the code which Mac_Focha posted here i am getting this error: 12-03 12:27:42.336: E/AndroidRuntime(3526): FATAL EXCEPTION: main 12-03 12:27:42.336: E/AndroidRuntime(3526): java.lang.IllegalArgumentException: width and height must be > 0 12-03 12:27:42.336: E/AndroidRuntime(3526):at android.graphics.Bitmap.createBitmap(Bitmap.java:687) 12-03 12:27:42.336: E/AndroidRuntime(3526): at android.graphics.Bitmap.createBitmap(Bitmap.java:666) 12-03 12:27:42.336: E/AndroidRuntime(3526): at android.graphics.Bitmap.createBitmap(Bitmap.java:633) 12-03 12:27:42.336: E/AndroidRuntime(3526): – Dardan Dec 03 '13 at 12:32
  • you are creating a new CustomView in OnClick.this newly created View is not attached to Window. get the reference of CustomView which is attached to Activity window and your work on this – Gopal Gopi Dec 03 '13 at 12:33
  • What do you mean that my view is not attached to the window ? I declared now the View as a field and i initialized it at oncreate method but still doesn't work the same error : protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_api); setWallpaper = (Button) findViewById(R.id.wallpaper); setWallpaper.setOnClickListener(this); viewWallpaper = (Button) findViewById(R.id.viewwallpaper); viewWallpaper.setOnClickListener(this); view = new CustomWallpaper(this); } – Dardan Dec 03 '13 at 12:39

2 Answers2

1
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null) {
        bgDrawable.draw(canvas);
    } else {
        canvas.drawColor(Color.WHITE);
    }
    view.draw(canvas);

and your bitmap is returnedBitmap

Maciej Boguta
  • 1,354
  • 1
  • 12
  • 15
  • added your code before b = convertToBitmap(view); but now i am having another error it saying IllegalArgumentException: width and height must be > 0 – Dardan Dec 03 '13 at 12:16
  • it means your view isnt inflated yet, or has size of 0,0 for some reason, try to investigate it with debugger orso – Maciej Boguta Dec 03 '13 at 12:23
0

then no need to create CustomView. try this. it may help you...

public void onClick(View view) {
    switch (view.getId()) {
    case R.id.wallpaper:
        Bitmap bitmap = getWallPaperBitmap();
        .......
        wallPaperManager.setBitmap(bitmap);
        break;

    default:
        break;
    }
}

public static Bitmap getWallPaperBitmap() {
    Bitmap bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawARGB(0, 0, 0, 0);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);
    // this style will fill the circle
    paint.setStyle(Paint.Style.FILL);
    // this style will draw Circle path.
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, 40, paint);
    return bitmap;
}
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • Thank you for trying to help me , but i am not trying to get view through xml but instead i want from java class which extends view you can check to my question the java class which extends view . – Dardan Dec 03 '13 at 12:45
  • And now you are very close to solve my problem now no problem has been found but after setting wallpaper only black background color has been set , can't see the red circle in the wallpaper thank you – Dardan Dec 03 '13 at 13:02