Я искал ответ и пробовал разные решения в течение довольно утомительного времени. Я пытаюсь разработать приложение для Android с вложенными фрагментами с одним фрагментом, содержащим вкладки. Проблема возникает, когда я пытаюсь установить содержимое TabSpec в другой фрагмент. Ну, сначала я разделил MainActivity на два фрагмента, разделенных функциональной областью, которая не является частью проблемы:
<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>
Сосредоточив внимание на возникающей проблеме, я опубликую код верхнего фрагмента. Класс ArticleUser выглядит следующим образом:
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);
}
}
Внутри article_user_fragment.xml я определяю TabHost, который должен содержать два других фрагмента:
<?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>
До сих пор у меня не было проблем с выполнением моего приложения. Теперь, когда я пытаюсь отредактировать внешний вид вкладки в классе ArticleUser.class, я получаю сообщение об ошибке, что представление не может быть найдено, когда я пытаюсь установить содержимое.
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;
}
}
Ошибка: Причина: java.lang.RuntimeException: Не удалось создать содержимое вкладки, поскольку не удалось найти представление с идентификатором 2131492950.
Я уже нашел это: Представления вкладок Android — не удалось создать содержимое вкладки, поскольку не удалось найти представление с идентификатором
и это: in-android">Почему я получаю сообщение об ошибке при попытке установить содержимое табуляции в Android?
Но эти решения не очень помогли мне. Вы действительно поможете мне здесь!
С уважением, Денис.