Android 使用addView()时,我的应用程序崩溃

Android 使用addView()时,我的应用程序崩溃,android,viewgroup,Android,Viewgroup,我是安卓的傻瓜。每当我使用ViewGroup addView()方法时,我的应用程序都会不断崩溃,但当我使用Activity addContentView()方法时,它会工作 我希望使用addView()而不是addContentView()的原因是,我希望能够在不需要的时候删除布局,这在addContentView()中是不可能的 代码: MainActivity.java protected void onCreate(Bundle savedInstanceState) { sup

我是安卓的傻瓜。每当我使用ViewGroup addView()方法时,我的应用程序都会不断崩溃,但当我使用Activity addContentView()方法时,它会工作

我希望使用addView()而不是addContentView()的原因是,我希望能够在不需要的时候删除布局,这在addContentView()中是不可能的

代码: MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //CAMERA VIEW
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    ViewGroup rootView = (ViewGroup) findViewById(R.layout.activity_main);
    LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

    //GRAPHICS VIEW
    glSurfaceView = new GLSurfaceView(this);
    glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
    glSurfaceView.setRenderer(new GLRenderer());
    //crash
    //rootView.addView(glSurfaceView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    //not crash
    addContentView(glSurfaceView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    glSurfaceView.setZOrderMediaOverlay(true);

    //CONTROLS VIEW
    View ctrlLayout = mLayoutInflater.inflate(R.layout.activity_controls, rootView);
        //crash
        //rootView.addView(ctrlLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        //not crash
    addContentView(ctrlLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    ctrlLayout.bringToFront();
    textView = (TextView)ctrlLayout.findViewById(R.id.text);
    rtTextView = (TextView)ctrlLayout.findViewById(R.id.rt_text);
    btnStart = (Button)ctrlLayout.findViewById(R.id.button1);
}
activity_main.xml

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

    <SurfaceView
        android:id="@+id/surfaceview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

活动中使用
findViewById()
之前,必须调用
setContentView()
。此外,
findViewById()
返回内容视图中包含的视图,这些视图由作为参数传递的id值标识,因此传递布局资源将永远不起作用

另一个选项是使用
LayoutInflater
,自己为视图充气:

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup rootView  = inflater.inflate(R.layout.main, null);

对于在这里尝试使用自定义视图跟踪错误的其他人,请仔细检查是否正确充气。以下更改解决了我的问题

错误:

View view = inflate(getContext(), R.layout.foo, this);
addView(view);
工作正常:

View view = inflate(getContext(), R.layout.foo, null);
addView(view);

我在使用attachToRoot=true时遇到了同样的问题

final View submitButton = LayoutInflater.from(this).inflate
            (R.layout.view_button_simple, rootView, true);
我不得不把它改成

final View submitButton = LayoutInflater.from(this).inflate
            (R.layout.view_button_simple, rootView, false);

成功了。我的应用程序不再崩溃,但现在addView()没有显示任何内容。有什么建议吗?我已经尝试更改glSurfaceView和ctrlView的布局参数以包装内容,但仍然不起作用。如果您使用上述方法(LayoutInflater),则必须调用setContentView(rootView);在onCreate的末尾。是的,我正在做你的方法。我试图调用setContentView(rootView),但当我称之为我的应用程序崩溃时。您似乎将视图作为其自身的子视图添加,但我无法告诉您添加位置,因为我没有代码。实际上使用View View=inflate(getContext(),R.layout.foo,this,false);这是正确的方法。
final View submitButton = LayoutInflater.from(this).inflate
            (R.layout.view_button_simple, rootView, true);
final View submitButton = LayoutInflater.from(this).inflate
            (R.layout.view_button_simple, rootView, false);