我一直在尝试制作我的第一个 Android 应用程序。我被这个奇怪的问题困扰了。过去有很多人遇到过同样的问题(例如 here 、 here 、 here 和很多其他地方),但似乎没有一个解决方案对我有用。
我的问题是,如果我设置像 setContentView(R.layout.MainActivity)
这样的布局文件,应用程序会在此函数上崩溃,但如果我直接设置 GLSurfaceView
作为内容 View 应用程序工作正好。我希望在同一屏幕上有一个 ListView
和 GLSurfaceView
这就是我尝试将其添加到 XML 中的原因。
这是我的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.example.camerafilters.CameraGLSurfaceView android:id="@+id/CameraGLSurfaceView" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <!-- <ListView android:id="@+id/ShaderList" android:layout_width="wrap_content" android:layout_height="wrap_content" /> --> </LinearLayout>
这是主要 Activity 的相关部分。请注意,CameraGLSurfaceView
是一个内部类。
public class CameraMainActivity extends Activity implements
SurfaceTexture.OnFrameAvailableListener
{
private CameraGLSurfaceView _cameraGLView;
/**
* Captures frames from an image stream as an OpenGL ES texture. The image stream may come from either camera preview or video decode.
* A SurfaceTexture may be used in place of a SurfaceHolder when specifying the output destination of a Camera or MediaPlayer object
*/
private SurfaceTexture _surface;
CameraGLRenderer _renderer;
private Camera _camera;
private ListView _shaderListView;
class CameraGLSurfaceView extends GLSurfaceView
{
CameraGLRenderer renderer;
public CameraGLSurfaceView(Context context, AttributeSet attrs)
{
super(context, attrs);
// Create an OpenGL ES 2.0 context
setEGLContextClientVersion(2);
// Set the Renderer for drawing on the GLSurfaceView
renderer = new CameraGLRenderer((CameraMainActivity)context);
setRenderer(renderer);
// Render the view only when there is a change in the drawing data
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
public CameraGLRenderer getRenderer()
{
return renderer;
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
_cameraGLView = new CameraGLSurfaceView(this, null);
// Create a GLSurfaceView instance and set it
// as the ContentView for this Activity.
setContentView(R.layout.activity_main);
_cameraGLView = (CameraGLSurfaceView) findViewById(R.id.CameraGLSurfaceView);
_renderer = _cameraGLView.getRenderer();
}
有什么地方我错了吗?
最佳答案
首先,查看 logcat 中的异常堆栈跟踪以找到确切的问题。
然后,猜测问题是 XML 中指定的 View 无法实例化:您的内部类需要是 public static
并且您需要在 XML 中正确引用它,例如
com.example.camerafilters.CameraMainActivity$CameraGLSurfaceView
尽管将其作为单独的类而不是内部类更清晰。
关于java - setContentView() 与 GLSurfaceView 一起使用时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23336444/