- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个带有 TableLayout
的 Fragment
。表中的数据来自 SQLite 数据库。 SQLite 数据库是从 MainActivity
中的 AsyncTask
中的 RESTful web 服务填充的。 Fragment
必须等待任务完成才能填充表。 Fragment
监听要调用的任务 onPostExecute()
方法。如果是,则调用 Fragment
中的方法 onLoadAndStoreComplete()
。这一切都有效。
我需要在 fragment 的 OnCreateView()
方法之外获取 TableLayout
的 View 。如果我可以在 onLoadAndStoreComplete
中获取 fragment 的 View ,那我就可以到达那里。
Same code as here.我从 MainActivity 获得了 mContext
,但是没有与之关联的 getView()
方法。
我试过:
- 创建类成员 rootView 并在 onCreateView() 中分配,但在 onLoadAndStoreComplete()
中,它为 null。
- 创建类成员 tableLayout 并在 onCreateView() 中分配,但在 onLoadAndStoreComplete()
中,它为 null。
- 在 onLoadAndStoreComplete()
中再次调用 this.getView(),但它返回 null。
- 在 onLoadAndStoreComplete()
中调用充气机,这有效,但我不知道在 .inflate()
调用中为容器使用什么
不明白为什么onLoadAndStoreComplete()
中rootView和tableLayout的类成员值为null
public class MyFragment extends Fragment implements OnLoadAndStoreCompleteListener {
private TableLayout tableLayout;
private View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mContext = this.getActivity();
rootView = inflater.inflate(R.layout.fragment_permits, container, false); // this works
tableLayout = (TableLayout) rootView.findViewById(R.id.main_table); // and this
...
}
@Override
public void onLoadAndStoreComplete() {
// rootView is null, ie not remembered from OnCreateView
View view = getView(); // null
LayoutInflater inflater = LayoutInflater.from(getActivity());
rootView = inflater.inflate(R.layout.fragment_permits, container, false); // container? don't know what it should be
// tableLayout is null
tableLayout.addView(tableRow, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
...
}
最佳答案
如果我对您的代码的理解正确,那么您的代码不遵守 Fragment 生命周期 或不同的 Fragment 实例失败有问题。
A.生命周期问题
Android 框架希望您的 Fragment 在 onCreateView()
方法中创建其 View 。在框架调用此方法后 View 变得可用。
可能是您的代码在 onCreateView()
之前调用了 onLoadAndStoreComplete()
,这导致了问题。
您需要提取用数据填充实际 View 的方法,因为它可以有 2 个入口点。请看下面的代码:
public class MyFragment extends Fragment implements OnLoadAndStoreCompleteListener {
private TableLayout tableLayout;
private View rootView;
private SomeDataClass loadedData;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
mContext = this.getActivity();
rootView = inflater.inflate(R.layout.fragment_permits, container, false);
tableLayout = (TableLayout) rootView.findViewById(R.id.main_table);
updateUi(); // If onLoadAndStoreComplete() was already called
// this will plug newly created view to returned data
}
@Override
public void onLoadAndStoreComplete() {
loadedData = ...; // Save loaded data here
updateUi(); // If onCreateView() was already called
// this will plug loaded data to the view
}
private void updateUi() {
if (rootView == null) { // Check if view is already inflated
return;
}
if (loadedData != null) {
// View is already inflated and data is ready - update the view!
...
}
}
}
B.不同实例失败
当您在前面提到的 fragment 的 2 个不同实例上操作时,可能会发生这种情况。
首先,一切看起来都是有序的:onCreateView()
在 onLoadAndStoreComplete()
之前调用。因此检查这些:
onCreateView()
的对象是否与调用onLoadAndStoreComplete()
的对象完全相同,您可以使用System.identityHashCode(this)
,在这两种方法中获取不同的值是一个不好的迹象如果发生这种情况,原因可能隐藏得更深一些,没有代码我无法就此给您准确的建议。你如何创建你的 fragment ?通过 XML 或手动添加,然后通过 FragmentManager 或通过 ViewPager 添加?
关于android - 如何在 OnCreateView() 之外获取 Fragment 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29607864/
我已经搜索了与此相关的每个主题,但没有一个对我有帮助...我是初学者,非常感谢您的回答...当我启动该应用程序时,一切都很好,但是当我第二次调用 HomeFragment 时,出现了问题。我试过了 F
我尝试将 Android 兼容性库(或现在称为支持库)v4 用于 pre-honeycomb android 设备。我明白,我应该使用 FragmentActivity 类,而不是使用 Fragmen
我正在创建一个选项卡式应用程序,但遇到了问题。当我单击我所在位置旁边的选项卡时,onCreateView() 运行。我的意思是,如果我有三个选项卡(设置、 map 和电机),如果我单击电机选项卡, m
我的选项卡有一个viewpager。我有三个选项卡,我在第一个选项卡中进行计算,将其存储在仍然是共享首选项的tinyDb 中。我还在第二个选项卡中进行了另一个计算,并将答案存储在tinydb中,然后通
我有一个带有 Activity 和 3 个可滑动选项卡( fragment )的应用程序。我在选项卡内创建了一些大量创建的 UI 元素,这些元素需要一些时间才能创建,第一次运行没问题,但每次我滑动它们
我不止一次看到这个问题在这里被问到,但我不知道如何解决我的问题。 我有一个用户执行此操作的应用: 1 - 打开 navigationDrawer 并选择一个选项(创建了一个 fragment )(我在
我创建了一个具有以下构造函数的自定义首选项 public CoordinatesPreference(Context context, AttributeSet attrs) { super(
我正在尝试构建一个包含多个 fragment 的应用程序,其中所有 fragment 都实现了 LoaderCallbacks。我一直在阅读 StackOverflow 上的帖子,其中大多数都指出要使
当我将 onCreateView() 中的 container 类型从 Viewgroup? 更改为 Viewgroup 时,我得到了这个错误: 这是为什么? 最佳答案 您收到错误,因为 ViewGr
我试图将 TimePicker 片段放入我创建的选项卡类中,但以下重写方法导致了问题: @Override public View onCreateView(LayoutInflater in
我遇到了一个奇怪的问题,onCreateView每次我导航到 TabLayout 中的相邻选项卡时都会被调用。这是我的代码: news_feed.java: private static TabLay
如果我有在片段的 onCreateView 中声明的变量;有没有办法可以从 onCreateView 外部访问这些变量? public View onCreateView(LayoutInflate
我当前的问题是我正在尝试触发 Animator 在两个 Activity 之间转换。我的目标是创建从一个 Activity 到另一个 Activity 的循环显示过渡。 我开始收到此错误:“无法在分离
我正在将数据从 Activity 发送到 fragment 并使用以下代码启动 fragment : if (conversation != null) { FragmentManager f
这里我写了一个简单的 Activity 和 fragment 。我改变了方向,因为它是 configuration change 调用,所以它再次调用了 Activity.onCreate() 但为什
我注意到我的一个 fragment 的 onCreateView() 从未被调用,我不知道为什么。我需要能够在 onResume() 中进行更改,但是那也从未被调用。 这是我添加 fragment 的
我正试图摆脱一个麻烦的异常。 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedIn
尝试将 RecyclerView 实现到我的应用程序中,我不明白为什么我会收到“未连接适配器;跳过布局”我想我清楚地在 onCreateView 中设置了它。其他一些帖子建议将其移至 Oncreate
我想知道 onCreateView() 中的容器参数是什么,因为当我将 View 膨胀到那个容器时,我想知道这是什么 viewGroup,它是我们将添加的 Activity 的 viewGroup f
我创建了一个应用程序,它使用了 antonyt 的 InfiniteViewpager。 我想在创建 View 时在 Imageview 上添加动画,但我的动画工作了几次,但并非总是如此。这是代码:
我是一名优秀的程序员,十分优秀!