我正在开发一个应用程序,我需要绘制并将其保存为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卡中 当我点击保存按钮时我的应用程序强制关闭请帮助我 在此先感谢
答案 0 :(得分:1)
我会supsect
FrameLayout v = (FrameLayout) findViewById(R.id.main_frame);
返回 null
使用调试器或记录
进行检查当您的视图不包含FrameLayout时,它是
如果此代码真的在第173行
public void onClickSave()
比这更令人怀疑,因为没有访问参考。 这可能是一些过时的代码和apk的暗示。尝试重建项目 (项目 - &gt;清除日食)
答案 1 :(得分:0)
在使用之前尝试构建绘图缓存:
v.buildDrawingCache();