Search code examples
javaandroidandroid-fragmentstabsfragment-tab-host

Android TabHost.TabSpec.setContent(...) in nested Fragments "could not find View with Id"


I've been searching for an answer and trying different solutions for a pretty exhausting time now. I am trying to design an android application with nested Fragments with one Fragment containing Tabs. The Problem occurs when I am trying to set the content of the TabSpec to another Fragment. Well, first I devided the MainActivity into two Fragments devided by a function area which is not part of the problem:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ScientificJournal"
android:weightSum="5">


<fragment
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:name="ba.uni.dego.scientificjournal.ArticleUser"
    android:id="@+id/workspace_user"
    tools:layout="@layout/article_user_fragment"
    android:layout_weight="2" />
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:focusableInTouchMode="false">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/b_test1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button4" />

</LinearLayout>

<fragment
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:name="ba.uni.dego.scientificjournal.ArticleEditor"
    android:id="@+id/workspace_editor"
    tools:layout="@layout/article_editor_fragment"
    android:layout_weight="2" />
</LinearLayout>

Focusing on the occuring issue I will post the Code of the top-Fragment. The ArticleUser-Class looks like this:

package ba.uni.dego.scientificjournal;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TextView;

public class ArticleUser extends Fragment{


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.article_user_fragment,
            container, false);
}
}

Inside of the article_user_fragment.xml I am defining a TabHost which should contain two other fragments:

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

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:showDividers="middle">
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <fragment
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:name="ba.uni.dego.scientificjournal.ArticleUserRead"
                android:id="@+id/f_user_read"
                android:layout_gravity="center"
                tools:layout="@layout/article_user_read_fragment"
                android:tag="Read" />

            <fragment
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:name="ba.uni.dego.scientificjournal.ArticleUserArchive"
                android:id="@+id/f_user_archive"
                android:layout_gravity="center"
                tools:layout="@layout/article_user_archive_fragment"
                android:tag="Archive" />
        </FrameLayout>
    </LinearLayout>
</TabHost>

Up to here I don't get any problems executing my application. Now when I am trying to edit the Tab-appearance in the ArticleUser.class I am getting the Error tha the View could not be found when I am trying to set the content.

package ba.uni.dego.scientificjournal;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TextView;

public class ArticleUser extends Fragment{


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.article_user_fragment,
            container, false);

    TabHost th =(TabHost) view.findViewById(android.R.id.tabhost);
    TabHost.TabSpec readerSpec = th.newTabSpec("Read");
    readerSpec.setIndicator("Read");
    readerSpec.setContent(R.id.subfragment_read);
    th.addTab(readerSpec);

    return view;
}
}

The error is: Caused by: java.lang.RuntimeException: Could not create tab content because could not find view with id 2131492950

I already found this: Android Tab Views - could not create tab content because could not find view with id

and this: Why do I get an error while trying to set the content of a tabspec in android?

But these solutions didn't help me a lot. You would really help me out here!

Best regards, Dennis.


Solution

  • Ok I finally found my answer here: http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html

    Returning a new FragmentTabHost in my ArticleUser.class instead of the View found by Id solved my issue.