Reputation: 274320
This is the first time that I draw something on android. I followed a tutorial on developers.android.com:
http://developer.android.com/training/custom-views/custom-drawing.html
And obviously I should create a custom view and override onDraw
and I can start drawing on the Canvas
parameter! So I created a view like this:
public class CanvasView extends View {
private Canvas canvas;
public Canvas getCanvas() {
return canvas;
}
public CanvasView (Context c) {
super(c);
}
@Override
protected void onDraw (Canvas c) {
canvas = c;
Paint p = new Paint();
p.setColor (Color.BLACK);
c.drawCircle (50, 50, 25, p);
}
}
As you can see I have a method to get the canvas object. This is because I want to be able to add stuff to the view from other code (not in this class). I think my code makes sense. I just want to draw a black circle!
When I run the app, it crashed immediately with a InvalidClassException
:
java.io.InvalidClassException: dfa; Incompatible class (SUID): dfa: static final long serialVersionUID =8874316054258000122L; but expected dfa: static final long serialVersionUID =0L;
at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2383)
at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1665)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:683)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1806)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1963)
at com.google.android.apps.plus.service.EsService.c(PG:5140)
at com.google.android.apps.plus.service.EsService.b(PG:5064)
at com.google.android.apps.plus.service.EsService.a(PG:2522)
at com.google.android.apps.plus.service.EsService.c(PG:2613)
at com.google.android.apps.plus.service.PackageAddedReceiver.onReceive(PG:37)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2488)
at android.app.ActivityThread.access$1500(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
I searched for questions abount InvalidClassException
s but I found nothing like this. They are all about deserialization stuff. I found the word serialVersionUID
which is relevant to serialization inn the call stack but my code doesn't do anything with serialization!
I think it might be because of the Canvas
field. If that is really the problem, is there any other way that I can draw stuff on the UI but without the use of drawable resource files? If this is not the problem, then what is? I don't know why a InvalidClassException
has anything to do with drawing.
Upvotes: 0
Views: 74
Reputation: 4305
It looks like that the serialVersionUID, as it wasn't explicitly defined in the code, was autogenerated to 8874316054258000122L, which it was not what it was expected.
Try to manually set the serialVersionUID to 0L, as expected by dfa
:
private static final long serialVersionUID = 0L;
Upvotes: 1