如何将自定义SurfaceView添加到XML布局

时间:2012-08-12 13:26:50

标签: android surfaceview android-xml

我尝试在XML中使用自己的SurfaceView,但我无法做到。我得到NullPointerException。 根据互联网,它应该是这样的:
活动:

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>

2 个答案:

答案 0 :(得分: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

    }
}

答案 1 :(得分:0)

你不能调用Canvas canvas = holder.lockCanvas();`

在oncreate flow完成之前

你应该在oncreate完成后调用它