Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在曲面上绘制onDraw()事件外部的视图_Android_Surfaceview - Fatal编程技术网

Android 在曲面上绘制onDraw()事件外部的视图

Android 在曲面上绘制onDraw()事件外部的视图,android,surfaceview,Android,Surfaceview,我有一个自定义类DrawingView,它扩展了SurfaceView。所有绘图当前都发生在onTouchEvent()-事件中。那么,我怎样才能在我的表面上画一些东西呢 我在activity_main.xml中这样定义了DrawingView: <com.example.standardbenutzer.adelpath.DrawingView android:id="@+id/surface" android:layout_width="fill_parent" android:lay

我有一个自定义类
DrawingView
,它扩展了
SurfaceView
。所有绘图当前都发生在
onTouchEvent()
-事件中。那么,我怎样才能在我的表面上画一些东西呢

我在activity_main.xml中这样定义了DrawingView:

<com.example.standardbenutzer.adelpath.DrawingView
android:id="@+id/surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"/>
但我得到:

java.lang.RuntimeException:无法启动活动组件信息{com.example.standardbenutzer.adelpath/com.example.standardbenutzer.adelpath.MainActivity}:java.lang.NullPointerException

这里有什么建议我可以在火山口外的水面上画画吗


因此,我的DrawingView扩展了SurfaceView,现在实现了SurfaceHolder.Callback接口。 因此,在不同的构造函数中,我添加了如下回调事件:

    public DrawingView(Context context) {
    super(context);
    surfaceHolder.addCallback(this);
}

public DrawingView(Context context, AttributeSet attrs){
    super(context, attrs);
    surfaceHolder.addCallback(this);
}

public DrawingView(Context context, AttributeSet attrs, int defStyle){
    super(context, attrs, defStyle);
    surfaceHolder.addCallback(this);
}
    @Override
public void surfaceCreated(SurfaceHolder holder){
    render(holder);
}
在onSurfaceCreated事件中,我渲染图像如下:

    public DrawingView(Context context) {
    super(context);
    surfaceHolder.addCallback(this);
}

public DrawingView(Context context, AttributeSet attrs){
    super(context, attrs);
    surfaceHolder.addCallback(this);
}

public DrawingView(Context context, AttributeSet attrs, int defStyle){
    super(context, attrs, defStyle);
    surfaceHolder.addCallback(this);
}
    @Override
public void surfaceCreated(SurfaceHolder holder){
    render(holder);
}

但这仍然在DrawingView类中。如何将绘图部分从DrawingView类移动到我的主要活动?例如,我想在单击按钮时渲染图像。我仍然在谷歌上找不到我如何做到这一点。也许这不是使用surfaceview的正确方式?有其他选择吗?

NPE发生在哪里?什么是空的?您是否正在等待启动
surfaceCreated()
回调?有关一些示例,请参见Grafika()。MultiSurfaceActivity在UI线程外做一些琐碎的动画。啊,谢谢你的提示。现在,我的MainActivity实现了SurfaceHolder.Callback,在surfaceCreated事件中我执行以下操作:
code DrawingView dV=(DrawingView)findViewById(R.id.surface);Canvas canv=dV.surfaceHolder.lockCanvas();油漆=新油漆();油漆。设置颜色(颜色。白色);canv.drawText(“你好,世界”,5,25,50100,油漆);dV.surfaceHolder.unlockCanvasAndPost(canv);dV.invalidate()现在我没有收到错误,但我的surfaceview上也没有发生任何事情