Android 来自findViewByID的Opengl es 2.0空指针

Android 来自findViewByID的Opengl es 2.0空指针,android,opengl-es,opengl-es-2.0,Android,Opengl Es,Opengl Es 2.0,我正在尝试使用以下教程,使用OpenGL ES 2.0设置应用程序: 因此,我能够成功地编译应用程序并在模拟器中运行,但一旦启动,应用程序就会崩溃。。。我只有2个布局文件: activity_main.xml: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@

我正在尝试使用以下教程,使用OpenGL ES 2.0设置应用程序:

因此,我能够成功地编译应用程序并在模拟器中运行,但一旦启动,应用程序就会崩溃。。。我只有2个布局文件: activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.openglproject1.MainActivity"
    tools:ignore="MergeRootFrame" />
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gamelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gamelayout"             //OVER HERE
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</RelativeLayout>
在OnCreate MainActivity的方法中:

protected void onCreate(Bundle savedInstanceState) {

        // Turn off the window's title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Super
        super.onCreate(savedInstanceState);

        // Fullscreen mode
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // We create our Surfaceview for our OpenGL here.
        glSurfaceView = new GLSurf(this);

        // Set our view.
        setContentView(R.layout.activity_main);

        // Retrieve our Relative layout from our main layout we just set to our view.
        RelativeLayout layout = (RelativeLayout) findViewById(R.id.gamelayout);

        // Attach our surfaceview to our relative layout from our main layout.
        RelativeLayout.LayoutParams glParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        layout.addView(glSurfaceView, glParams);
    }

返回一个空指针。。。我使用的全部代码都在上面的链接中,但是如果你愿意,我也可以将它粘贴到这里,尽管它有点大。我只是想知道我是否需要任何额外的游戏布局文件或任何东西?。。。这对我来说是非常混乱的,所以请不要犹豫提供任何提示!我将非常感谢您的帮助

将活动布局设置为

setContentView(R.layout.activity_main);
其来源是:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.openglproject1.MainActivity"
    tools:ignore="MergeRootFrame" />
很明显,错误就在这里


更改此行
setContentView(R.layout.activity\u main)
setContentView(R.layout.fragment\u main)并让我知道它是否有效

在您的activity_main中没有任何内容涉及R.id.gamelayout。所述布局位于fragment_main.xmlI的内部。我已经尝试用fragment_main替换activity_main的内容,或者将代码从fragment_main添加到activity_main等。。。但这一切都是徒劳的。实际上,我不明白为什么在教程中我总是只使用fragment_main.xml来添加一些视图,比如按钮等,而通过创建一个活动,我得到了两个布局。。。我仍然不太确定如何解决这个问题:/你不必更换/移除。。只需参考与活动相关的布局中指定的内容。如果这个视图在你的布局中很好,如果不是,你会得到一个NPE。如果我只改变这一行,问题仍然存在。但是如果我在findViewById之前将内容更改为fragment_main,并在它之后将其设置回activity_main,那么应用程序最终不会崩溃。但这是正确的方法吗?实际上,应用程序并没有像想象的那样画一个三角形,所以我猜还是有问题
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gamelayout"             //OVER HERE
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</RelativeLayout>