2

我尝试在 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。
表面视图:

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

2 回答 2

5

我找到了答案:实现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

    }
}
于 2012-08-12T14:09:54.873 回答
0

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

在 oncreate 流程完成之前,

你应该在 oncreate 完成后调用它

于 2012-08-12T14:13:01.657 回答