2

I'm trapped in "No tab content FrameLayout found in XXX" error. Please give a help.

My code goes follow:

MainActivity

package com.example.demo;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.example.demo.fragments.StateFragment;


public class MainActivity extends AppCompatActivity {
    private FragmentTabHost mTabHost;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setSupportActionBar((Toolbar) findViewById(R.id.toolBar));
        initTabHost();
    }


    private void initTabHost(){
        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                StateFragment.class, null);
    }
}

activity_main.xml

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Title" />

    </android.support.v7.widget.Toolbar>

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

And logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demo/com.example.demo.MainActivity}: java.lang.IllegalStateException: No tab content FrameLayout found for id 2131492952
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.IllegalStateException: No tab content FrameLayout found for id 2131492952
            at android.support.v4.app.FragmentTabHost.ensureContent(FragmentTabHost.java:215)
            at android.support.v4.app.FragmentTabHost.setup(FragmentTabHost.java:201)
            at com.example.demo.MainActivity.initTabHost(MainActivity.java:51)
            at com.example.demo.MainActivity.onCreate(MainActivity.java:23)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

I found this post by Google. It came to this point with two version of v4 library. So I checked my

build.gradle:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
}

And nothing on my libs directory. Therefore, no two version v4 lib make conflict.

After debugging, I found that FrameLayout with id "realtabcontent" can be found in MainActivity. When the code ran into the step:

private void ensureContent() {
        if (mRealTabContent == null) {
            mRealTabContent = (FrameLayout)findViewById(mContainerId);
            if (mRealTabContent == null) {
                throw new IllegalStateException(
                        "No tab content FrameLayout found for id " + mContainerId);
            }
        }
    }

in the FragmentTabHost.java, the method findViewById(mContainerId) return null. So confusing!!

So help me please~ Orz

Amareesh
  • 368
  • 2
  • 5
  • 19
Lawrence Ching
  • 423
  • 7
  • 16

2 Answers2

6

Your problem is that you are referencing your tabhost from android resources @android:id/tabhost and the framelayout from your XML with @+id/realtabcontent so the system can't inflate them.

The right way:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<android.support.v7.widget.Toolbar
    android:id="@+id/toolBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Title" />

</android.support.v7.widget.Toolbar>



<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_below="@id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

</android.support.v4.app.FragmentTabHost>

And in your MainActivity:

 mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
    mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
            StateFragment.class, null);
Marta Tenés
  • 2,102
  • 1
  • 13
  • 22
  • Thank you for your help. But I am still confused. I coded according to the page:http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html and other website like:http://www.programering.com/a/MjNygDNwATM.html. I found it that they put a FrameLayout outside the FragmentTabHost and name it a id "readtabcontent", and they worked. – Lawrence Ching Aug 23 '15 at 08:10
  • Note that your code is not the same as the code in the page you referenced. In this tutorial they use a FragmentTabHost with a TabWidget and then a FrameLayout above for showing the content of the fragment. – Marta Tenés Aug 24 '15 at 06:11
  • In your code you're using on the one hand, a TabWidget and FrameLayout within a FragmentTabHost and in the other hand a FrameLayout and that is wrong. Check your code and the code of your example and see the differences. – Marta Tenés Aug 24 '15 at 06:18
0

I had the same issue when I migrated my sample from Eclipse to Android Studio.

I solved this problem by:

  1. Copying the android-support-v4.jar from my Eclipse project folder to Android Studio
  2. Removing the compile ‘com.android.support:support-v4:22.2.0’ from build.gradle
  3. Keeping compile fileTree(include: ['*.jar'], dir: 'libs')
Joshua W
  • 1,103
  • 12
  • 29
Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57