我已经实现了自定义 Android SurfaceView 并在为其添加回调时遇到错误。
这是我的课程代码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
}
});
}
}
当我将此视图添加到layout.xml
,例如:
<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" />
显示时我会遇到错误Graphical Layout
:未能实例化。(在插件:com.android.ide.eclipse.adt),当运行这个项目时,我会有异常。在 LogCat 中查看时,我看到了 Java Null Point Exception。
奇怪的是:如果我删除holder.addCallback(new SurfaceHolder.Callback() {...
没有遇到错误。
这看起来也很奇怪,请告诉我如何解决这个问题。
@已编辑:问题是因为当我时getHolder
,它不返回surface holder
,所以持有人为空,我不知道为什么。
谢谢 :)