0

我正在开发一个应用程序,我需要在其中绘制并将其保存为 sd 卡中的图像

 public void onClickSave()
        {

            FrameLayout v = (FrameLayout) findViewById(R.id.main_frame);
            v.setDrawingCacheEnabled(true);

            v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            v.layout(0, 0, v.getWidth(), v.getHeight());
            v.buildDrawingCache(true);
            Bitmap bm = Bitmap.createBitmap(v.getDrawingCache());
            v.setDrawingCacheEnabled(false);
            if (bm != null) {
                try {
                    String path = Environment.getExternalStorageDirectory().toString();
                    OutputStream fOut = null;
                    File file = new File(path, "screentest.jpg");
                    fOut = new FileOutputStream(file);
                    bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                    fOut.flush();
                    fOut.close();                     
                }
                catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(), e.toString(),1000).show();
                }
            }

并且函数调用发生在这里

 Button btn_save=(Button) findViewById(R.id.button3);
        btn_save.setOnClickListener(new OnClickListener() 
        {

            public void onClick(View v) 
            {
                // TODO Auto-generated method stub

                drawView.onClickSave();

            }
        });

我在日志中得到错误:

05-23 12:46:25.413: W/dalvikvm(669): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-23 12:46:25.423: E/AndroidRuntime(669): FATAL EXCEPTION: main
05-23 12:46:25.423: E/AndroidRuntime(669): java.lang.NullPointerException
05-23 12:46:25.423: E/AndroidRuntime(669):  at com.page2.DrawView.onClickSave(DrawView.java:173)
05-23 12:46:25.423: E/AndroidRuntime(669):  at com.page2.Draw$3.onClick(Draw.java:60)
05-23 12:46:25.423: E/AndroidRuntime(669):  at android.view.View.performClick(View.java:2408)
05-23 12:46:25.423: E/AndroidRuntime(669):  at android.view.View$PerformClick.run(View.java:8816)
05-23 12:46:25.423: E/AndroidRuntime(669):  at android.os.Handler.handleCallback(Handler.java:587)
05-23 12:46:25.423: E/AndroidRuntime(669):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-23 12:46:25.423: E/AndroidRuntime(669):  at android.os.Looper.loop(Looper.java:123)
05-23 12:46:25.423: E/AndroidRuntime(669):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-23 12:46:25.423: E/AndroidRuntime(669):  at java.lang.reflect.Method.invokeNative(Native Method)
05-23 12:46:25.423: E/AndroidRuntime(669):  at java.lang.reflect.Method.invoke(Method.java:521)
05-23 12:46:25.423: E/AndroidRuntime(669):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-23 12:46:25.423: E/AndroidRuntime(669):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-23 12:46:25.423: E/AndroidRuntime(669):  at dalvik.system.NativeStart.main(Native Method)

我的xml是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/white" >
<FrameLayout
 android:id="@+id/main_frame"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="@android:color/white" android:layout_weight="0.1">

<ImageView
     android:id="@+id/imageView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
      /> 

</FrameLayout>
<RelativeLayout
 android:id="@+id/relativeLayout1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
   android:layout_weight="0.9" android:layout_gravity="bottom" android:layout_marginTop="-10dp">
 <Button
     android:id="@+id/button2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentTop="true"
     android:layout_centerHorizontal="true"
     android:text="Redo" />
 <Button
     android:id="@+id/button1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentLeft="true"
     android:layout_alignParentTop="true"
     android:text="Undo" />
 <Button
     android:id="@+id/button3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentRight="true"
     android:layout_alignParentTop="true"
     android:text="Save" />
 </RelativeLayout>
</LinearLayout>This is my xml 

给我一些其他方式将画布绘制作为图像存储在 sd 卡中我的应用程序在我单击保存按钮时强制关闭请帮助我提前致谢

4

2 回答 2

1

我怀疑

       FrameLayout v = (FrameLayout) findViewById(R.id.main_frame);

返回

使用调试器或日志记录检查

当您的视图不包含 FrameLayout 它是

  • 不是由您的 xml 创建的
  • 或“main_frame”已在其他函数中删除

如果这段代码真的在第 173 行

  public void onClickSave()

这比这很可疑,因为没有获得参考。这可能暗示代码和 apk 已过时。尝试重建项目(project->clean on eclipse)

于 2012-05-23T09:01:16.503 回答
0

在使用它之前尝试构建你的绘图缓存:

v.buildDrawingCache();
于 2012-05-23T08:56:47.123 回答