GLSurfaceView 父级为 null,即使它嵌套在 LinearLayout 中
我有一个名为 GLView 的自定义视图,它扩展了 GLSurfaceView 类。在此 GLView 内部,我想访问父线性布局中包含的其他 TextView。当我调用 this.getParent 时,它返回 NULL,所以我查看了一下,eclipse 确实说 mparent 为 null。有谁知道为什么?
调用视图
package org.kizik.WLTBO;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class WholettheballoutActivity extends Activity {
GLView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score);
view = (GLView) findViewById(R.id.mySurfaceView);
// You can use a FrameLayout to hold the surface view
/*FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(view);
// Then create a layout to hold everything, for example a RelativeLayout
RelativeLayout relativeLayout= new RelativeLayout(this);
relativeLayout.addView(frameLayout);
relativeLayout.addView(score);
setContentView(relativeLayout);*/
}
@Override
protected void onPause() {
super.onPause();
view.onPause();
}
@Override
protected void onResume() {
super.onResume();
view.onResume();
}
}
XML 文件的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/parent" >
<TextView android:id="@+id/textView2"
android:text = "Points 12345 Time:"
android:gravity="center_horizontal"
android:textSize="22sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<org.kizik.WLTBO.GLView
android:id="@+id/mySurfaceView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
Activity 类可能不需要,但这里是 GLView 类中的构造函数
public GLView(Context context) {
super(context);
// Uncomment this to turn on error-checking and logging
//setDebugFlags(DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS);
renderer = new GLRenderer(context, view);
setRenderer(renderer);
}
public GLView(Context context, AttributeSet attrs){
super(context,attrs);
view = this.getParent();
renderer = new GLRenderer(context, view);
setRenderer(renderer);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Activity 中执行
setContentView()
之前,请先尝试扩展布局。然后,您可以检查默认布局行为是否将 GLSurfaceView 附加到 LinearLayout:您实际上无法从
onDraw() 内部更改父视图(线性布局)或渲染器中的任何其他视图
、onSurfaceChanged()
或onSurfaceCreated()
方法,因为它们都是由 GL 线程调用的。只有主/UI 线程(在 Activity 中调用onCreate()
的线程)可以更改 UI。例如,如果您尝试在onSurfaceCreated()
中执行((TextView)view.findViewById(R.id.textView2)).setText("Some text");
,这样做会导致异常更好的方法是从活动内部设置渲染器:
然后在渲染器中:
Handler 排队等待 UI 线程在下次循环时执行。还有其他方法可以在其他线程和UI线程之间传递消息,但这虽然不是立即显而易见的,但对于对 UI 进行单个异步更新的非 UI 线程来说最有意义
编辑:必须在要使用它的线程中声明处理程序(除非您正在做一些时髦的事情参见此处)
Try expanding the layout first before you do
setContentView()
in activity. You can then check whether the default layout behavior is attaching the GLSurfaceView to the LinearLayout:You can't actually change the parent view (the linear layout) or any other views in your renderer from inside the
onDraw()
,onSurfaceChanged()
oronSurfaceCreated()
methods as they are all called by the GL thread. Only the main/UI thread (the one that callsonCreate()
in your activity) can change the UI. If you tried for example to do((TextView)view.findViewById(R.id.textView2)).setText("Some text");
inonSurfaceCreated()
, it would cause an exceptionA better way to do this would be to set the renderer from inside the activity:
Then in renderer:
Anything inside the handleMessage method of a Handler gets queued for the UI thread to execute next time it loops round. There are other ways to pass messages between other threads and the UI thread, but this one although not immediately obvious, makes most sense for non-UI threads making single asynchronous updates to the UI
EDIT: Handler has to be declared in the thread it is to be used in (unless you're doing something funky see here)
文档说
获取此视图的父级。请注意,父级是 ViewParent,
不一定是 View。
The documentation says
Gets the parent of this view. Note that the parent is a ViewParent and
not necessarily a View.