在活动查找片段查看。 Android的findFragmentByTag(字符串标签)返回null [英] Finding Fragment View on Activity. Android findFragmentByTag(String tag) returns null

查看:116
本文介绍了在活动查找片段查看。 Android的findFragmentByTag(字符串标签)返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个额外的片段来我的活动,但似乎我不能得到的片段,也不认为。

I'm trying to get a Spinner View from an added Fragment to my activity but it seems that I cannot get the fragment nor the view.

我有一个活动增加了一个片段到它的的LinearLayout ,片段的布局是基于额外来自活动的意图。一切都显示正常,我可以看到该片段的所有意见,但因为当我打电话某种原因, findFragmentByTag(字符串标签)返回null,因此我不能调用getView()

I have an Activity which adds a Fragment to it's LinearLayout, the fragment's layout is based on the 'extra' that comes from the intent of the Activity. Everything is displayed correctly and I can see all the views of the fragment but for some reason when I call findFragmentByTag(String tag) it returns null and thus I cannot call getView().

下面是我如何加入片段code到我的活动:

Here is how I am adding the fragment code to my activity:

...onCreate(){
    ...
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    Fragment fr = new Fragment(){
        @Override
        public View onCreateView(LayoutInflater inflater,
                ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(layoutID, container, false);
        }
    };
    ft.add(R.id.formsListActivity_Container, fr, "form_fragment_tag");

    ft.commit();
    ...

}

然后我试图让这样的片段,并获得了微调,以添加字符串数组:

Then I try to get such fragment and get a the spinner view to add the string array:

Fragment f = fm.findFragmentByTag("form_fragment_tag");
Spinner spinner = (Spinner) f.getView().findViewById(R.id.city_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
        R.array.city_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

不过,我得到一个空异常,因为findFragmentByTag()返回null

But I get a null exception because findFragmentByTag() returns null

我是什么做错了吗?我已经验证了codeA几次的标签是相同的。 我也确保了微调的id是在XML布局文件正确,且片段设置正确的XML布局,因为我可以看到它(如果注释从片段部分获取视图)。

What am I doing wrong? I've verified the code a few times and the tags are the same. I've also make sure the spinner's id is in the XML layout file correctly, and the fragment is loading the correct XML layout because I can see it (If a comment the get view from fragment part).

推荐答案

我已经解决了这个问题,似乎同时基于活性和片段通过时的findFragmentById(的生命周期)中的onCreate被执行()片段还没有被实际创建',我通过增加一个简单的按钮布局和制作方法上的按钮,点击后的微调将充满数据测试这一点。

I have resolved the problem, it seems that based on the life cycle of both the activity and the fragment by the time the findFragmentById() is executed in the onCreate() the fragment has not been 'actually created', I tested this by adding a simple button to the layout and making a method that when clicked on the button the spinner will be filled with data.

不过,我当然不希望用户单击加载按钮,以填补微调,而是我已经在我的片段实施公共无效onActivityCreated()方法。

But of course I don't want the user to click a 'Load' button to fill the spinner, instead I have implemented on my fragment the public void onActivityCreated() method.

有关引用这是我的$ C $的片段C:

For reference here is my code of the fragment:

 Fragment fr = new Fragment(){

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(layoutID, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Spinner spinner = (Spinner) this.getView().findViewById(R.id.city_spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.city_list, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

 };

这篇关于在活动查找片段查看。 Android的findFragmentByTag(字符串标签)返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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