I am trying to FileOutputStream a Bitmap drawn in a canvas, but Logcat gives me :
09-17 14:47:40.203: E/AndroidRuntime(2362): FATAL EXCEPTION: main
09-17 14:47:40.203: E/AndroidRuntime(2362): Process: com.example.drawv2, PID: 2362
09-17 14:47:40.203: E/AndroidRuntime(2362): java.lang.NullPointerException
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.graphics.Bitmap.compress(Bitmap.java:1002)
09-17 14:47:40.203: E/AndroidRuntime(2362): at com.example.drawv2.Scribbler$1.onClick(Scribbler.java:65)
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.view.View.performClick(View.java:4780)
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.view.View$PerformClick.run(View.java:19866)
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.os.Handler.handleCallback(Handler.java:739)
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.os.Handler.dispatchMessage(Handler.java:95)
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.os.Looper.loop(Looper.java:135)
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.app.ActivityThread.main(ActivityThread.java:5257)
09-17 14:47:40.203: E/AndroidRuntime(2362): at java.lang.reflect.Method.invoke(Native Method)
09-17 14:47:40.203: E/AndroidRuntime(2362): at java.lang.reflect.Method.invoke(Method.java:372)
09-17 14:47:40.203: E/AndroidRuntime(2362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
09-17 14:47:40.203: E/AndroidRuntime(2362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Here is my Scribbler class :
public class Scribbler extends Activity {
DrawView drawView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
drawView = (DrawView) inflater.inflate(R.layout.layoutview, null);
drawView = new DrawView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
drawView.setLayoutParams(layoutParams);
drawView.setBackgroundColor(Color.WHITE);
linearLayout.addView(drawView);
drawView.requestFocus();
drawView.setDrawingCacheEnabled(true);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Bitmap b = Scribbler.this.drawView.getDrawingCache();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(getFilesDir());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
b.compress(CompressFormat.PNG, 95, fos);
}
});
}
}
I'm a bit confused since generally Logcat says which variable is the cause of NullPointerException, but here I don't get anything.
For information, I had the Cannot refer to the non-final local variable button defined in an enclosing scope
error on the drawView
, because I only needed its value when I clicked the button1
. I assume the exception is still due to the fact that my drawView = null
but I have no idea how to fix this.
Thank you in advance.