我有一个需要捕获客户签名然后在ImageView中呈现的应用程序。为了进行概念验证,我编写了一个小应用,该应用的布局顶部是GestureOverlayView,然后是“保存签名”按钮,然后是下面的ImageView。当我按下“保存签名”按钮时,它应该从GestureOverlayView创建一个图像并将其显示在ImageView中。下面是代码/布局,然后是问题列表:

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <android.gesture.GestureOverlayView android:id="@+id/GestureView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#FFFFFF"
        android:fadeEnabled="false"
        android:gestureStrokeType="multiple"
        android:gestureStrokeWidth="1.5"
        android:gestureColor="#000000"
        android:fadeOffset="600000"
        android:eventsInterceptionEnabled="false"
        android:alwaysDrawnWithCache="true">
    </android.gesture.GestureOverlayView>
    <Button android:id="@+id/SubmitButton"
        android:text="Save Signature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </Button>
    <ImageView android:id="@+id/ImageView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#FFFFFF">
    </ImageView>
</LinearLayout>


码:

public class TestActivity extends Activity {
    ImageView iv;
    GestureOverlayView gv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        iv = (ImageView)findViewById(R.id.ImageView);
        gv = (GestureOverlayView)findViewById(R.id.GestureView);

        Button submitButton = (Button)findViewById(R.id.SubmitButton);
        submitButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap b = Bitmap.createBitmap(gv.getDrawingCache());
                iv.setImageBitmap(b);
            }
        });
    }
}


每次在createBitmap()方法上都得到一个Null Pointer异常。我相信是因为gv.getDrawingCaches()返回null。如您所见,图形缓存已启用,因此应返回“ SOMETHING”!

这是logcat:

FATAL EXCEPTION: main
java.lang.NullPointerException
    at android.graphics.Bitmap.createBitmap(Bitmap.java:358)
    at com.pgov.barcode.TestActivity$2.onClick(TestActivity.java:42)
    at android.view.View.performClick(View.java:2408)
    at android.view.View$PerformClick.run(View.java:8816)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)


任何和所有帮助将不胜感激!

最佳答案

显然我回答了我自己的问题。即使我使用过:

android:alwaysDrawnWithCache="true"


我将代码更改为:

gv.setDrawingCacheEnabled(true);
Bitmap b = Bitmap.createBitmap(gv.getDrawingCache());
iv.setImageBitmap(b);
gv.setDrawingCacheEnabled(false);


而且有效!不知道为什么XML标志不会影响它...

10-06 14:59