gpt4 book ai didi

android - 无法从 fragment 中获取父 View

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:07 24 4
gpt4 key购买 nike

我需要从与 TextView 在 XML 文件中位于同一平面上的 ListFragment 更新 TextView 。但是两次尝试都只返回 null:

@Override
public void onCreate(Bundle ice)
{
super.onCreate(ice);

...

mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.entry_row, null, from, to, 0);
setListAdapter(mAdapter);

mTextView = (TextView) getActivity().findViewById(R.id.tv); // try #1

mPercentView = (TextView) ((ViewGroup)getView().getParent()).findViewById(R.id.tv); //try #2
// (not tested at the same time)
}

这是 XML:

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


<TextView
android:id="@+id/info_log_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="@dimen/object_padding"
android:textSize="24sp"
android:textIsSelectable="true" />

<fragment
android:id="@+id/view_log_fragment"
android:name="fragment name..."
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/info_log_view" />

</RelativeLayout>

谢谢!

最佳答案

这解决了我的问题希望它对你也有帮助:所以覆盖类 onActivityCreated 并在其中编写以下代码:

View myActivityView=(RelativeLayout)getActivity().findViewById(R.id.parentContainer);

请注意,RelativeLayout 是我包含 Textview 的主要布局。当你在你的 fragment 中有这个时,你可以访问你的 fragment 父 View ,现在只需要通过下面的代码获取 TextView :

TextView myTextView=(TextView)myActivityView.findViewById(R.id.myTextView);

希望这对您有所帮助。

关于android - 无法从 fragment 中获取父 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16554198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com