SurfaceView崩溃 - 为什么?

时间:2013-01-04 16:41:36

标签: android surfaceview

现在我正在尝试测试SurfaceView。它显示在我的预览中。它显示了一个带有我自定义SurfaceView('RollView')名称的灰色背景。每次我尝试测试它。它只是立即崩溃。当我删除com.hovanky.roll.RollView xml标记时,它的工作原理。但我失去了我的表面视图。我做错了什么?

在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>

在我的活动中,这扩展了活动。我给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) {
}

1 个答案:

答案 0 :(得分:1)

将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...