Android:如何获取Fragment的视图

2024-01-07

在课堂上,我以编程方式添加一个片段。

该片段包含一个按钮,我想要一个 onClick 实现。

这是我的课程:

public class ShareProduct extends SherlockFragmentActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.share_product);

        FragmentManager fragmentManager = getSupportFragmentManager();
        final FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();

        ProductFragment1 fragment = new ProductFragment1();
        fragmentTransaction.add(R.id.share_product_parent, fragment);
        fragmentTransaction.commit();

        Button done_button = (Button) fragment.getView().findViewById(
                R.id.done_product_1);
        done_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                ProductFragment2 fragment = new ProductFragment2();
                fragmentTransaction
                        .replace(R.id.share_product_parent, fragment);
                fragmentTransaction.commit();
            }
        });
    }
}

我的 ProductFragment1 类是:

public class ProductFragment1 extends SherlockFragment {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater
                .inflate(R.layout.share_product_1, container, false);
        return view;
    }
}

我的主类使用的布局是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/share_product_parent"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="right|center_vertical"
    android:orientation="horizontal" >

</RelativeLayout>

我的 ProductFragment1 使用的布局是:

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

    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp" />

    <EditText
        android:id="@+id/et1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv1"
        android:textSize="25dp" />

    <Button
        android:id="@+id/done_product_1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/et1"
        android:text="Done" />

</RelativeLayout>

问题是:

我在主类中的这一行收到 NullPointerException:

Button done_button = (Button) fragment.getView().findViewById(R.id.done_product_1);
done_button.setOnClickListener ....

当我寻找什么时getView()在片段返回上。我发现它返回的视图来自onCreateView();

我检查了它不返回 null。

谢谢


你在这里走错了路。你得到NPE因为Fragments视图尚未创建。Fragments也有一个生命周期,就像Activities.

你应该做的是分配OnClickListener在你的里面Fragments onViewCreated()方法。从那里......您应该创建一个回调并通知您的托管Activity事件的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:如何获取Fragment的视图 的相关文章

随机推荐

  • Matlab/Octave中所有内置符号列表

    在 Mathematica 中 我们可以获得所有内置函数的名称 例如 List通过执行命令 Names List 此外 Names context 列出指定上下文中的所有符号 例如 Names Global 给出所有内置符号的名称 以及用户
  • 在 C++ 中使用 C# 接口或在 C# 中使用 C++ 接口

    我正在开发一个解决方案 其中有一个必须由 C 解决方案和 C 解决方案使用的接口 C 语言 我想知道 实现这一目标的最佳方法是什么 我在 C 项目中使用的 C 接口 我将在 C 项目中使用的 C 接口 您还可以指示我应该如何进行最佳解决方案
  • 张量流中的Python_io

    我在使用张量流时遇到问题 我想用TFRecordWriter 如下 with tf python io TFRecordWriter testing filename as tfrecord writer do sth 但我收到错误 Att
  • 如何求多个矩阵中对应元素的最大值?

    我有四个具有相同维度的矩阵 比方说 A 1 2 5 4 2 9 B 4 5 9 8 0 1 C 5 3 9 0 4 0 D 5 9 1 0 9 3 如何求四个矩阵中所有对应元素的最大值 在我的示例中 结果应如下所示 maxABCD 5 9
  • 如何在matlab中为条形图添加数据标签

    例如 代码 x 3 6 2 9 5 1 bar x 为此 我需要在每个栏的顶部添加数据标签 我知道我必须使用 TEXT 关键字 但我不知道如何实现它 这是一个简单的解决方案text x 3 6 2 9 5 1 bar x ylim 0 ma
  • 批量更新 Jenkins 项目的最佳方法是什么?

    我们有数百个 Jenkins 项目 大部分是从几个模板创建的 通常需要对所有项目进行相同的更改 例如今天我需要添加一个构建后步骤以在最后删除工作区 接下来 我需要更改将构建结果复制到 Nexus 存储库的共享驱动器的步骤 将此类批量更改应用
  • VSCode Intellisense 在 Javascript 代码上奇怪的自动完成

    我刚刚使用 VSCode 编写 JS 文件时遇到了一些奇怪的行为 看到我正在尝试访问hasOwnProperty 蓬松物体上的方法 VSCode 不会自动完成或建议我的代码行中的任何内容 此时我已经认为我做错了什么 并且我可能无法通过我的对
  • 使用jquery解析部分html字符串

    I use ajax 从我的服务器获取一些 HTML 页面 返回包含完整的 HTML 结果 但我只对该文档中的一个非常具体的 div 感兴趣 唯一给出的事情是我的 ajax success 函数返回一个 JSON 对象 我制作了一个 PHP
  • 如何在 Flask 中缓存大型机器学习模型?

    这是我面临的情况 我刚刚编写了一个 Flask 应用程序 人们可以输入他们想要的文本评论 我的应用程序将从我们的数据集中返回最相似的评论 所以基本上这是一个 NLP 项目 机器学习模型已经训练好了 现在的问题是该模型大约有 2 5GB 每次
  • 替换二维数组的列值中的子字符串

    我正在尝试使用str replace 删除 我的 出于value4数组数组中的元素 然而 str replace my myarray 并没有改变任何东西 Does str replace 不适用于二维数组 我的示例数据和编码尝试 arra
  • TransactionScope/SqlTransaction 超时扩展

    一旦事务开始 是否可以延长事务的超时 使用 SQL Server 超时对于 SQL Server 来说是 外部 的 因此 SQL Server 无法影响它 所以不幸的是 不
  • 如何将字典绑定到gridview?

    是否可以自动将字典绑定到 Gridview 我最接近的是 Dictionary
  • 浏览器是否跟踪活动计时器 ID?

    浏览器是否跟踪活动setInterval and setTimeout身份证 或者这完全取决于开发人员来跟踪 如果它确实跟踪它们 是否可以通过 BOM 访问 由开发人员来跟踪 您可以通过使用 setTimeout setInterval 函
  • bootstrap 3将文本内容换行在div内以进行水平对齐

    My post title here could be misleading first have a look at HTML i have currently 正如您所看到的 每一列的文本内容都会溢出到下一列 其次 它们中的每一个都不是
  • UITableViewCell 中的 UILabel 位置第一次尝试失败

    我刚刚开始接触 iOS 开发 所以我希望我能在这里获得正确的详细信息 我有一个UILabel 加载到通过 xib 创建的表格单元格中 实际上有几个标签 其中之一的长度不同 因此包装高度也不同 heightForRowAtIndexPath等
  • 为什么 jQuery UI 不隐藏具有隐藏父元素的元素?

    我一直在开发一个单页应用程序 并注意到我的下拉菜单有时在本应隐藏的情况下保持打开状态 我调查了为什么会发生这种情况 并发现如果父元素被隐藏 则使用扩展的 jQuery UI 隐藏元素hide功能毫无价值 This fiddle http j
  • 使用 rlang 创建带有双花括号 {{ 的函数(data.table 结构)

    是否可以使用 rlang 结构来执行函数data table 例如 没有数据表 library data table library dplyr iris 1 1 2 NA iris 3 3 4 NA test dt lt function
  • asynsPipe 生成 null 作为第一个值

    当在 Angular 中使用异步管道时 不会立即触发事件 http 请求或任何有延迟的可观察值 得到的第一个值是null为什么会发生这种情况 如何避免这种情况
  • 数据框的减法运算

    我有2个数据框df1 and df2 df1 lt data frame c1 c a b c d c2 c 1 2 3 4 df2 lt data frame c1 c c d e f c2 c 3 4 5 6 gt df1 c1 c2
  • Android:如何获取Fragment的视图

    在课堂上 我以编程方式添加一个片段 该片段包含一个按钮 我想要一个 onClick 实现 这是我的课程 public class ShareProduct extends SherlockFragmentActivity protected