2013-01-04 87 views
1

現在我試圖測試SurfaceView。它顯示在我的預覽中。它用我自定義的SurfaceView('RollView')的名稱顯示灰色背景。每次我嘗試測試它。它只是立即崩潰。當我刪除com.hovanky.roll.RollView xml標記時,它可以工作。但是我失去了我的表面視圖。我究竟做錯了什麼?SurfaceView崩潰 - 爲什麼?

在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

更改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... 
+0

這工作我愛你 – Hovanky