现在我正在尝试测试 SurfaceView。它出现在我的预览中。它显示带有我的自定义 SurfaceView(“RollView”)名称的灰色背景。每次我尝试测试它。它会立即崩溃。当我删除 com.hovanky.roll.RollView xml 标签时,它起作用了。但是我失去了我的表面 View 。我做错了什么?
在 xml 中我放入了我的自定义 SurfaceView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.hovanky.roll.RollView
android:id="@+id/rollview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</FrameLayout>
在我的 Activity 中,它扩展了 Activity 。我为我的 SurfaceView 提供句柄
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRollView = (RollView) findViewById(R.id.rollview);
然后,我绘制了自定义 SurfaceView 的框架。
class RollView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
public RollView(Context context) {
super(context);
holder= getHolder();
holder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
最佳答案
将您的 RollView 类更改为:
class RollView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
public RollView(Context context) {
super(context);
holder= getHolder();
holder.addCallback(this);
}
public RollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
//your code here...
关于android - SurfaceView 崩溃 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14161161/