添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Im a noob at android. My app keeps crashing whenever I use ViewGroup addView() method but works when I use the Activity addContentView() method.

The reason I want to use addView() instead of addContentView() is that I want to be able to remove the layouts when not needed which is not possible with addContentView().

Code: 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>

activity_controls.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" >
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="14dp"
        android:text="@string/hello_world" />
    <TextView
        android:id="@+id/rt_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="18dp"
        android:layout_marginTop="14dp"
        android:text="@string/str_rttext" />
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/rt_text"
        android:layout_centerHorizontal="true"
        android:text="@string/str_title"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <Button
        android:id="@+id/button1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/btn_start" />
</RelativeLayout>

EDIT:

I changed my code as suggested by FD_

ViewGroup rootView = mLayoutInflater.inflate(R.layout.activity_main, null); Now when I call setContentView(rootView) at the end of onCreate function, it crashes.

Here is the logcat:

02-16 08:00:28.142: I/dalvikvm(2153): threadid=1: stack overflow on call to Landroid/view/View;.isLayoutDirectionInherited:Z
02-16 08:00:28.142: I/dalvikvm(2153):   method requires 12+20+4=36 bytes, fp is 0xb02fe320 (32 left)
02-16 08:00:28.152: I/dalvikvm(2153):   expanding stack end (0xb02fe300 to 0xb02fe000)
02-16 08:00:28.152: I/dalvikvm(2153): Shrank stack (to 0xb02fe300, curFrame is 0xb0303ec4)
02-16 08:00:28.152: D/AndroidRuntime(2153): Shutting down VM
02-16 08:00:28.152: W/dalvikvm(2153): threadid=1: thread exiting with uncaught exception (group=0xb1aeab90)
02-16 08:00:28.932: D/dalvikvm(2153): GC_FOR_ALLOC freed 117K, 6% free 3266K/3448K, paused 80ms, total 86ms
02-16 08:00:29.382: D/dalvikvm(2153): GC_FOR_ALLOC freed 356K, 11% free 3423K/3844K, paused 46ms, total 47ms
02-16 08:00:29.402: E/AndroidRuntime(2153): FATAL EXCEPTION: main
02-16 08:00:29.402: E/AndroidRuntime(2153): Process: com.example.previewcamera, PID: 2153
02-16 08:00:29.402: E/AndroidRuntime(2153): java.lang.StackOverflowError
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5713)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153):     at android.view.ViewGroup.resetRes
02-16 08:00:29.632: D/dalvikvm(2153): GC_FOR_ALLOC freed 548K, 16% free 3386K/4000K, paused 54ms, total 55ms

For others that end up here trying to track down that error using custom views, double check that you're inflating correctly. The following change resolved my issues.

Error:

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

Works fine:

View view = inflate(getContext(), R.layout.foo, null);
addView(view);
                Actually using View view = inflate(getContext(), R.layout.foo, this, false); is the correct way to go.
– joe1806772
                Aug 23, 2018 at 16:29

You have to call setContentView() before you can use findViewById() in your Activity. Additionally, findViewById() returns views that are included in the content view and identified by the id value passed as the argument, so passing a layout resource will never work.

Another option is to inflate the View yourself, using LayoutInflater:

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup rootView  = inflater.inflate(R.layout.main, null);
                That did the job. My app is not crashing anymore but now the addView() is not displaying anything. Any suggestions?
– kredj
                Feb 15, 2014 at 23:27
                And I already tried to change the layout params of glSurfaceView and ctrlView to WRAP_CONTENT but still not working.
– kredj
                Feb 15, 2014 at 23:41
                If you are using the approach above (LayoutInflater), you'll haver to call setContentView(rootView); at the end of onCreate.
– FD_
                Feb 16, 2014 at 9:04
                Yes, I'm doing your approach. I tried to call setContentView(rootView) but whenevr I call it my app crash.
– kredj
                Feb 16, 2014 at 11:20
                You seem to be adding a View as a child of itself, but I cannot tell you where, because I don't have the code.
– FD_
                Feb 16, 2014 at 13:23

I had the same problem while using attachToRoot = true

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

I had to change it to

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.