【发布时间】:2012-08-07 02:20:40
【问题描述】:
我已经实现了自定义 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,所以持有人为空,我不知道为什么。
谢谢:)
【问题讨论】:
-
发布日志猫。检查持有人是否为空。单击包名称以红色显示的行。这将需要完全停止。
-
你也可以调试并在holder上设置断点,看是否为null。
-
请发布 getHolder()。它可能无法正常工作。
-
@CodeDroid 哦,是的,你说得对,
holder为空,我以前不这么认为。但是,为什么它是空的,请帮助我。谢谢:) -
如果你写了它,你应该知道什么是返回 null 或者它是如何返回 null 的。
标签: android android-layout surfaceview