I want to convert a linearlayout to an image.I am using the following code for that purpose,but i am getting nullpointerexception.Please tell me how to solve this problem.This is the code:
public class AndroidWebImage extends Activity {
ImageView bmImage;
LinearLayout view;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (LinearLayout)findViewById(R.id.screen);
bmImage = (ImageView)findViewById(R.id.image);
view.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false); // clear drawing cache
bmImage.setImageBitmap(b);
};
}
I am getting exception in this line:
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
This is the error log:
09-13 12:19:26.061: W/dalvikvm(551): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
09-13 12:19:26.112: E/AndroidRuntime(551): Uncaught handler: thread main exiting due to uncaught exception
09-13 12:19:26.593: E/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.velosys.interview_preparation/com.velosys.interview_preparation.activities.Certificate}: java.lang.NullPointerException
09-13 12:19:26.593: E/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
09-13 12:19:26.593: E/AndroidRuntime(551): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
09-13 12:19:26.593: E/AndroidRuntime(551): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
09-13 12:19:26.593: E/AndroidRuntime(551): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
09-13 12:19:26.593: E/AndroidRuntime(551): at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 12:19:26.593: E/AndroidRuntime(551): at android.os.Looper.loop(Looper.java:123)
09-13 12:19:26.593: E/AndroidRuntime(551): at android.app.ActivityThread.main(ActivityThread.java:4363)
09-13 12:19:26.593: E/AndroidRuntime(551): at java.lang.reflect.Method.invokeNative(Native Method)
09-13 12:19:26.593: E/AndroidRuntime(551): at java.lang.reflect.Method.invoke(Method.java:521)
09-13 12:19:26.593: E/AndroidRuntime(551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-13 12:19:26.593: E/AndroidRuntime(551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-13 12:19:26.593: E/AndroidRuntime(551): at dalvik.system.NativeStart.main(Native Method)
09-13 12:19:26.593: E/AndroidRuntime(551): Caused by: java.lang.NullPointerException
09-13 12:19:26.593: E/AndroidRuntime(551): at android.graphics.Bitmap.createBitmap(Bitmap.java:358)
09-13 12:19:26.593: E/AndroidRuntime(551): at com.velosys.interview_preparation.activities.Certificate.onCreate(Certificate.java:76)
09-13 12:19:26.593: E/AndroidRuntime(551): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-13 12:19:26.593: E/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
09-13 12:19:26.593: E/AndroidRuntime(551): ... 11 more
09-13 12:19:26.661: I/dalvikvm(551): threadid=7: reacting to signal 3
09-13 12:19:26.661: E/dalvikvm(551): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
Thanks in advance.
You can’t do this in onCreate. The layout has not yet been displayed. You can do it later in the Activity’s lifecylce, e.g. onWindowFocusChanged or use a ViewTreeObserver which I prefer as it’s more in keeping with OOP.
http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html