gpt4 book ai didi

java - 框架布局 : How to add other Views on top of GLSurfaceView?

转载 作者:行者123 更新时间:2023-11-29 22:07:32 25 4
gpt4 key购买 nike

我是 Andriod 的新手,很难在我的 GLSurfaceView 上添加控件( View )。我希望将其添加到角落。 (说右下角)最初我想添加像小部件一样的操纵杆,但起初我想添加“左”、“右”、“上”、“下”按钮。我创建了一个布局 XML 文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<View
android:id="@+id/viewControls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/relativeLayout1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="140dp" >
</RelativeLayout>

<Button
android:id="@+id/buttonR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/relativeLayout1"
android:text="R" />

<Button
android:id="@+id/buttonL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="L" />

</RelativeLayout>

在我的 Activity 中,我尝试如下:

public class MyOpenGL2Activity extends Activity {

private GLSurfaceView mGLview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

mGLview=new MyOpenGL2SurfaceView(this);

FrameLayout flayout=new FrameLayout(this);
flayout.addView(mGLview);

View controller=findViewById(R.id.viewControls);

flayout.addView(controller);


setContentView(flayout);
}
...

但是应用程序崩溃说

05-05 14:40:08.015: W/dalvikvm(2258): threadid=1: thread exiting with uncaught exception (group=0x40a841f8) 05-05 14:40:08.023: E/AndroidRuntime(2258): FATAL EXCEPTION: main 05-05 14:40:08.023: E/AndroidRuntime(2258): java.lang.RuntimeException: Unable to start activity ComponentInfo{ausoft.opengl/ausoft.opengl.MyOpenGL2Activity}: java.lang.NullPointerException 05-05 14:40:08.023: E/AndroidRuntime(2258): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955) 05-05 14:40:08.023: E/AndroidRuntime(2258): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) 05-05 14:40:08.023: E/AndroidRuntime(2258): at android.app.ActivityThread.access$600(ActivityThread.java:122) 05-05 14:40:08.023: E/AndroidRuntime(2258): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146) 05-05 14:40:08.023: E/AndroidRuntime(2258): at android.os.Handler.dispatchMessage(Handler.java:99) 05-05 14:40:08.023: E/AndroidRuntime(2258): at android.os.Looper.loop(Looper.java:137) 05-05 14:40:08.023: E/AndroidRuntime(2258): at android.app.ActivityThread.main(ActivityThread.java:4340) 05-05 14:40:08.023: E/AndroidRuntime(2258): at java.lang.reflect.Method.invokeNative(Native Method) 05-05 14:40:08.023: E/AndroidRuntime(2258): at java.lang.reflect.Method.invoke(Method.java:511) 05-05 14:40:08.023: E/AndroidRuntime(2258): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-05 14:40:08.023: E/AndroidRuntime(2258): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-05 14:40:08.023: E/AndroidRuntime(2258): at dalvik.system.NativeStart.main(Native Method) 05-05 14:40:08.023: E/AndroidRuntime(2258): Caused by: java.lang.NullPointerException 05-05 14:40:08.023: E/AndroidRuntime(2258): at android.view.ViewGroup.addView(ViewGroup.java:3165) 05-05 14:40:08.023: E/AndroidRuntime(2258): at android.view.ViewGroup.addView(ViewGroup.java:3152) 05-05 14:40:08.023: E/AndroidRuntime(2258): at ausoft.opengl.MyOpenGL2Activity.onCreate(MyOpenGL2Activity.java:35) 05-05 14:40:08.023: E/AndroidRuntime(2258): at android.app.Activity.performCreate(Activity.java:4465) 05-05 14:40:08.023: E/AndroidRuntime(2258): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-05 14:40:08.023: E/AndroidRuntime(2258): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919) 05-05 14:40:08.023: E/AndroidRuntime(2258): ... 11 more

有什么建议吗?

最佳答案

您正在尝试 findViewById(R.id.viewControls); 但您之前没有调用过 setContentView()... View viewControls 在哪里 ?


如果你愿意,你可以像这样创建 View

View controller = new View(this.getApplicationContext());

或者其他...

关于java - 框架布局 : How to add other Views on top of GLSurfaceView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10462950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com