I have implemented custom Android SurfaceView and meet error when I add callback for it.
Here is my code for class SurfaceView
:
public class AndroidSurface extends SurfaceView {
public SurfaceHolder holder;
public AndroidSurface(Context context, AttributeSet attrs) {
super(context, attrs);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
});
}
}
When I add this view to layout.xml
, for example :
<com.app.AndroidSurface
android:id="@+id/surfaceView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/rightBtn"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1" />
I will met error when show in Graphical Layout
: failed to instantiate. (at Plugin : com.android.ide.eclipse.adt) and when run this project, I will have exception. When view in LogCat, I see that Java Null Point Exception.
The strange thing is : if I remove holder.addCallback(new SurfaceHolder.Callback() {...
no error met.
This looks strange too me, please tell me how to fixed this.
@ Edited : and the problem because when I getHolder
, it doesn't return surface holder
, so holder is null, I don't know why.
Thanks :)