poh*_*yun 9 android surfaceview
我是java和OpenGL的新手.
我正在尝试使用能够同时显示3d对象的相机预览屏幕.通过api演示中的示例,我认为在api演示中组合示例的代码就足够了.但不知何故它不起作用.强制我在启动时关闭,错误被称为空指针异常.有人可以和我分享我哪里出错了以及如何从那里开始.我如何为代码组合如下所示:
myoverview.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.opengl.GLSurfaceView
android:id="@+id/cubes"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<SurfaceView
android:id="@+id/camera"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
myoverview.java
import android.app.Activity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.Window;
public class MyOverView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
// camera view as the background
SurfaceView cameraView = (SurfaceView) findViewById(R.id.camera);
cameraView = new CameraView(this);
// visual of both cubes
GLSurfaceView cubesView = (GLSurfaceView) findViewById(R.id.cubes);
cubesView = new GLSurfaceView(this);
cubesView.setRenderer(new CubeRenderer(false));
// set view
setContentView(R.layout.myoverview);
}
}
Run Code Online (Sandbox Code Playgroud)
GLSurfaceView.java
import android.content.Context;
class GLSurfaceView extends android.opengl.GLSurfaceView {
public GLSurfaceView(Context context) {
super(context);
}
}
Run Code Online (Sandbox Code Playgroud)
注意 :
我没有列出其余的文件,因为它们只是api演示的副本.cameraView引用camerapreview.java示例,CubeRenderer引用CubeRenderer.java和Cube.java示例.任何帮助,将不胜感激.
抱歉,由于格式错误,没有意识到编码不合适.
小智 4
使用 .xml 时出现空指针异常的原因是因为您实际上在 java 代码中创建了新视图..而不是使用您可能已传递属性的 .xml 文件中的视图(如果您确实传递了在属性中是..)..新视图显然会有一个空值..从而抛出一个空指针异常...例如-
cubesView = new GLSurfaceView(this);
如果您已经在包含 FrameLayout 的 .xml 文件中创建了视图,则实际上不需要在代码中使用。
| 归档时间: |
|
| 查看次数: |
19402 次 |
| 最近记录: |