Reputation: 303
Right now i'm trying to test SurfaceView. It shows up in my Preview. It shows a gray background with the name of my custom SurfaceView ('RollView'). Each time i try to test it. It just crashes immediately. When i remove the com.hovanky.roll.RollView xml tag, it works. But i lose my surfaceview. What am i doing wrong?
In xml I put in my custom 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>
In my activity, that extends activity. I give handles to my SurfaceView
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRollView = (RollView) findViewById(R.id.rollview);
Then, I draw up a skeleton of my custom 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) {
}
Upvotes: 1
Views: 1192
Reputation: 132982
Change your RollView class as:
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...
Upvotes: 1