android - 无法在 Android 中设置 fragment 的 ID

标签 android layout fragment

我在尝试为我的 fragment 提供 XML 中的 ID 时遇到问题,但我似乎无法做到这一点。我尝试按照教程进行操作,但 id 似乎未设置为 fragment 。我正在使用提供的滑动 + 部分模板。这是我的主要 Activity 的 xml:

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.

-->

<android.support.v4.view.PagerTitleStrip
    android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#33b5e5"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    android:textColor="#fff" />

<Fragment
    android:id="@+id/account_information_fragment"
    android:name="com.example.AccountInformationFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</android.support.v4.view.ViewPager>

然后在我的 fragment 类中我使用它来设置布局:

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

并且该布局不包含任何标签。任何帮助将不胜感激

最佳答案

我想我找到了问题所在。首先,Fragment 标签需要以小写的 F 开头,如下所示:

好:

<fragment>

差:

<Fragment>

其次,恐怕我忘记了 Toast 在尝试显示任何非字符串时会返回 false。你将不得不使用

String.valueOf(fragment.getId());

为了让它显示在 Toast 中。

我不知道为什么这在一开始不起作用,因为我相信我之前为标签尝试了 f 和 F。设置 android:tag 帮助我调试它,因为它自然返回一个字符串。

关于android - 无法在 Android 中设置 fragment 的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14863676/

相关文章:

android:包含布局时出现问题

android - E/CameraService : Permission Denial: can't use the camera pid=9410, uid=10230 CameraFragment

Android Studio 无法解析 org.jetbrains.kotlin :kotlin-gradle-plugin:1. 7.10

android - facebook android api getUsername 返回 null

ios - 如何创建一个顶部是 Collection View 而底部是列表的可 ScrollView ?

java - Android HttpEntry 读取在 Droid x 上挂起

android - 如何更改 android 中的选项卡宽度和高度以适用于所有设备

Android 在 Fragment 中添加 Tab

java - 更新到Android 3.6.1后出现错误

java - 第一次尝试了解 Intent