onAttach活动为null [英] onAttach activity is null

查看:133
本文介绍了onAttach活动为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建一个片段,我遇到getActivity()为空。
因此,要缩小问题,我不停地活动的本地副本中onAttach(活动活动),根据定义,当它连接到一个活动。

In creation of a fragment, I encountered getActivity() to be null. So to narrow down the problem, I kept a local copy of activity in onAttach(Activity activity), which by definition is when it is attached to an activity.

然而,我登录onAttach活性,它仍然是空

However, I logged the activity in onAttach, and it is still null.

我只跑入2.3.6及以下这个问题。

I'm only running into this problem in 2.3.6 and below.

这是一个已知的问题与支持包?

Is this a known problem with support package?

推荐答案

从Android的参考:

From Android reference:

该系列名为带来片段长达恢复状态的方法是:

The series of methods called to bring a fragment up to resumed state are:


  • onAttach(活动),一旦片段与其活动相关的调用。

  • 的onCreate(捆绑)呼吁做片段的初始创建。

  • onCreateView(LayoutInflater,ViewGroup中,束)创建并返回与片段相关联的视图层次。

  • onActivityCreated(包)告诉其活动已经完成了自己的Activity.onCreate()的片段。

  • onViewStateRestored(包)告诉所有的视图层次的保存状态已经恢复的片段。

  • 调用onStart()使得对用户可见的片段(根据它含有活性被启动)。

  • onResume()使得与用户交互的片段(基于包含活动正在恢复)。

粗体方式应该就是那间getActivity没有返回null了。

The bold method should be the one where getActivity doesn't return null anymore.

onAttach 方式不应该被用来调用活动对象的方法,它应该被用来初始化回调接口。这些接口的例子可以发现 rel=\"nofollow\">。

the onAttach method should not be used to call methods of the activity object, It should be used to initialise callback interfaces. An example of these interfaces can be found here.

这篇关于onAttach活动为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆