2012-08-12 128 views
1

我嘗試在XML中使用我自己的SurfaceView,並且我無法做到這一點。我得到NullPointerException。 根據互聯網就應該是這樣的:
活動:如何將自定義SurfaceView添加到XML佈局

package editor; 

import android.app.Activity; 
import android.os.Bundle; 

import com.example.balls_menu_v1.R; 

public class EditorActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.editor); 
    EditorView ev = (EditorView) findViewById(R.id.editorView); 

} 
} 

如果我評論findViewById我得到NullPointerException異常。
SurfaceView:

package editor; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.util.AttributeSet; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class EditorView extends SurfaceView { 

    public EditorView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public void onFinishInflate() { 
     super.onFinishInflate(); 
     SurfaceHolder holder = getHolder(); 
     Canvas canvas = holder.lockCanvas(); 
     canvas.drawColor(Color.GREEN); 
     holder.unlockCanvasAndPost(canvas); 
    } 
} 

佈局:editor.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <editor.EditorView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/editorView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
    /> 
</RelativeLayout> 

回答

3

我找到答案:實現SurfaceHolder.Callback,加SurfaceView的所有3個構造函數,並添加getHolder().addCallback(this);到每個構造函數。
代碼:

public class EditorView extends SurfaceView implements SurfaceHolder.Callback{ 

    public EditorView(Context context) { 
     super(context); 
     getHolder().addCallback(this); 
     // TODO Auto-generated constructor stub 
    } 

    public EditorView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     getHolder().addCallback(this); 
     // TODO Auto-generated constructor stub 
    } 

    public EditorView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     getHolder().addCallback(this); 
     // TODO Auto-generated constructor stub 
    } 

    public void doDraw() { 
     SurfaceHolder holder = getHolder(); 
     Canvas canvas = holder.lockCanvas(); 
     canvas.drawColor(Color.GREEN); 
     holder.unlockCanvasAndPost(canvas); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) {} 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     doDraw(); 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 

    } 
} 
0

你不能叫帆布帆布= holder.lockCanvas();`

的OnCreate中流動之前完成,

你應該在OnCreate做完後把它

+0

我在創建曲面時調用它。它工作正常。 – zie1ony 2012-08-13 06:26:54

相關問題