我有一个没有保存功能的绘图程序。我想知道我是否可以使用此代码?
// TODO Auto-generated method stub
OutputStream outStream = null;
File file = new File(extStorageDirectory, "000.PNG");
try {
outStream = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
Toast.makeText(ThePatch.this, "Saved", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(ThePatch.this, e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(ThePatch.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
它保存文件但只显示黑屏。根据日志猫我在第237行得到一个FC。
第237行
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
logcat的
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): FATAL EXCEPTION: main
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): java.lang.NullPointerException
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at www.hotapp.com.thepatch.html.ThePatch.onOptionsItemSelected(ThePatch.java:237)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at android.app.Activity.onMenuItemSelected(Activity.java:2195)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at android.view.View$PerformClick.run(View.java:8816)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at android.os.Handler.handleCallback(Handler.java:587)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at android.os.Looper.loop(Looper.java:123)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at java.lang.reflect.Method.invoke(Method.java:521)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at dalvik.system.NativeStart.main(Native Method)
这是我的onDraw
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
我从here.
获取了代码答案 0 :(得分:0)
所以你的代码试图在这里访问一个空指针:
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): java.lang.NullPointerException
05-09 21:19:10.405: ERROR/AndroidRuntime(11437): at www.hotapp.com.thepatch.html.ThePatch.onOptionsItemSelected(ThePatch.java:237)
修复此问题。