0

我想将线性布局转换为图像。我为此目的使用以下代码,但我得到了 nullpointerexception。请告诉我如何解决这个问题。这是代码:

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);   

};
}

我在这一行遇到异常:

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

这是错误日志:

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

提前致谢。

4

2 回答 2

1

您不能在 onCreate 中执行此操作。布局尚未显示。您可以稍后在 Activity 的生命周期中执行此操作,例如 onWindowFocusChanged 或使用我更喜欢的 ViewTreeObserver,因为它更符合 OOP。

http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html

于 2012-09-13T09:01:08.340 回答
0

奇怪的是,绘图缓存似乎为 getDrawingCache() 返回了 null。

也许您可以在不使用缓存的情况下尝试这种方法。

Bitmap bitmap = Bitmap.createBitmap( view.getLayoutParams().width, view.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
Canvas canvas = new Canvas(bitmap);
view.layout(0, 0, view.getLayoutParams().width, view.getLayoutParams().height);
view.draw(canvas);
于 2012-09-13T08:21:41.233 回答