0

ok i think it's throwing an exception. unfortunately i am not getting an error. it loads on the emulator and my phone but won't perform. it just says, "The application MyApp (process com.myapp) has stopped unexpectedly. Please try again." and then a ForceClose button. :(

once again here is my code:

public void onClick(DialogInterface dialog, int id) {
    try {
        MyActivity.this.setWallpaper(mContext.getDrawingCache()); 
        finish();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

on my own research i saw someone getting the bit map from getDrawingCache and converting it to a bitmap? but the documentation on view.getDrawingCache says it is already a bitmap, and setWallpaper is supposed to set a bitmap... i am out of ideas here.

John Parker
  • 54,048
  • 11
  • 129
  • 129
user580162
  • 73
  • 1
  • 4
  • 1
    In future please take the time to format the code in your question appropriately (using the "{}" editor control). – John Parker Jan 19 '11 at 15:55
  • 2
    Please, check out and try to understand the error, and also post the error (so the exception) here. As you've got all this in a try-catch, there is a change that your code is wrong in a completely different spot.... – Nanne Jan 19 '11 at 16:00
  • 2
    Use logcat to view the actual exception. – Nick Jan 19 '11 at 16:00
  • middaparka - it looked like it was formatted when i wrote it. when i hit send it bunched up. don't know why that happened but do PLEASE excuse me. – user580162 Jan 19 '11 at 16:14
  • Nanne, i am not very good at understanding the the debugger. It looks like maybe the bitmap is not being passed? but i don't know why it wouldn't. – user580162 Jan 19 '11 at 16:16
  • the mContext is a view that at this point in the app's execution is holding an image selected from a gallery. what is actually happening is that there is an image on screen and when clicked the image's ontouch event fires an alert with two choices. the first choice is what you see here and every other part of the app is working fine when i test with a toast message in place of the setwallpaper method. seems to be in that one line of code where the problem is. – user580162 Jan 19 '11 at 16:20
  • mContext is a View? mContext should be a Context. Go to your debug perspective, run your app through debug mode, and then check your LogCat, and paste your stack trace for the exception here. It will tell you exactly at what line it is failing, and the cause. – Kevin Coppock Jan 19 '11 at 16:23

1 Answers1

2

What is mContext? getDrawingCache() is a method of the View class, and will only return a valid Bitmap if you first call setDrawingCacheEnabled(true) or if you call buildDrawingCache(). You also must make sure that the View went through a layout pass and that its width and height are > 0.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • i said earlier that mContext is a view but that was a mistake. mContext is an ImageSwitcher object. you are saying i must setDrawingCacheEnable(true). so i do this in the onTouchListener or before? – user580162 Jan 19 '11 at 18:42
  • Also is it good practice to use it as i have it or should i move MyContext.getDrawingCache() into a bitmap variable first? – user580162 Jan 19 '11 at 18:52
  • Romain Guy! I wish I could buy you a cold one and shake your hand! setDrawingCacheEnabled(true) was the ticket!! Thank you! – user580162 Jan 19 '11 at 19:48