0

linearlayout を画像に変換したいのですが、そのために次のコードを使用していますが、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

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 に答える