总是返回null由一个FragmentStatePagerAda

总是返回null由一个FragmentStatePagerAda

本文介绍了片段getView()总是返回null由一个FragmentStatePagerAdapter创建片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读了很多关于碎片。发现有检索片段观看,因为空总是返回,但没有回答解决我的问题问题的其他人。我想要做的就是创建一个图片库。我有一个片段持有图像视图。为了说明我用的android.support.v4.view.ViewPager片段。而喂 ViewPager 我用 android.support.v4.app.FragmentStatePagerAdapter

现在的问题是,当一切都建立和图像显示我想将当前显示的图像保存到磁盘。所以,我需要获得当前片段ImageView的,但我不能因为fragment.getView()总是为空,与该片段相关联的活动为空过,我想不通为什么它。这里是一些code,看是否有人可以帮助这里:

这是我的片段:

 公共类ImageViewURLFragment扩展片段
{
    字符串_url;

    @覆盖
    公共查看onCreateView(LayoutInflater充气,
            ViewGroup中的容器,包savedInstanceState)
    {
        //最后两个参数保证的LayoutParams充气
        //正常。
        查看rootView =新ImageViewURL(getActivity(),_url);

        返回rootView;
    }

    公共无效setImageURL(字符串imgUrl的)
    {
        _url = imgUrl的;
    }
}
 

和我这个适配器:

 公共类ImageViewURLCustomAdapter扩展FragmentStatePagerAdapter
{
    名单<字符串> _urls =新的ArrayList<字符串>();

    公共ImageViewURLCustomAdapter(FragmentManager FM)
    {
        超(FM);
    }

    @覆盖
    公共片段的getItem(int i)以
    {
        ImageViewURLFragment片段=新ImageViewURLFragment();
        fragment.setImageURL(_urls.get(ⅰ));

        返回片段;
    }

    公共字符串getItemUrl(int i)以
    {
        返回_urls.get(ⅰ);
    }

    @覆盖
    公众诠释getCount将(){
        返回_urls.size();
    }

    @覆盖
    公共CharSequence的getPageTitle(INT位置){
        回到对象+(位置+ 1);
    }

    公共无效addImageURL(字符串URL)
    {
        _urls.add(URL);
    }
}
 

这是主要的活动中,注意用get评论:

 公共类MainActivity扩展FragmentActivity
{
    ImageViewURLCustomAdapter _iva = NULL;
    ViewPager _vp = NULL;

    @覆盖
    保护无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        _iva =新ImageViewURLCustomAdapter(
                        getSupportFragmentManager());
        _iva.addImageURL(http://upload.wikimedia.org/wikipedia/commons/b/b4/Saturn_(planet)_large.jpg);
        _iva.addImageURL(http://planetthreesixty.com/sites/default/files/planet.jpg);

        _vp =(ViewPager)findViewById(R.id.viewpager);
        _vp.setAdapter(_iva);
    }


    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单)
    {
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项)
    {
        如果(item.getItemId()== R.id.saveToSD)
        {
            INT指数= _vp.getCurrentItem();
            片段片段= _iva.getItem(指数);

            //这就是我需要的片段来看,但始终返回null
                    查看查看= fragment.getView();
            ImageView的IV =(ImageView的)view.findViewWithTag(ImageView的);
            BMP位= iv.getDrawingCache();


        }


        返回super.onOptionsItemSelected(项目);
    }
}
 

对不起,长源,我已经清除一切我可以,但我想present全弹零件。可能有人猜是什么hapenning?

在此先感谢。

解决方案

这是正在发生的事情,因为你期待 _iva.getItem(指数); 返回片段 ViewPager 使用对应于指定索引的页面。这不会发生,因为在 ViewPager 已经叫的getItem 方法来获得它需要的片段,您的电话后, 的getItem 方法,你会得到一个新的 ImageViewURLFragment 实例。这个新的实例不依赖于活动 getActivity()返回)和它的观点没有被创建。

当你使用 FragmentStatePagerAdapter 试试下面的code,以获得当前可见片段

 如果(item.getItemId()== R.id.saveToSD){
     INT指数= _vp.getCurrentItem();
     片段片段= _vp.getAdapter()instantiateItem(_vp,指数)。
     // ...
 

I have been reading a lot about fragments. Have found other people having problems retrieving fragments view because null was always returned but no answer solved my problem. What I'm trying to do is create a image gallery. I have a Fragment that holds a image view. To show the fragments I use a android.support.v4.view.ViewPager. And to feed the ViewPager I use a android.support.v4.app.FragmentStatePagerAdapter.

The problem is that when everything is build and the images are shown I want to save the current shown image to disk. So I need to get the current fragment imageview but I can't because fragment.getView() always is null, the activity associated with the fragment is null too and I can't figure out why is it. Here some code to see if anyone could help here:

This is my Fragment:

public class ImageViewURLFragment extends Fragment
{
    String _url;

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState)
    {
        // The last two arguments ensure LayoutParams are inflated
        // properly.
        View rootView = new ImageViewURL(getActivity(), _url);

        return rootView;
    }

    public void setImageURL(String imgURL)
    {
        _url=imgURL;
    }
}

And this my adapter:

public class ImageViewURLCustomAdapter extends FragmentStatePagerAdapter
{
    List<String> _urls=new ArrayList<String>();

    public ImageViewURLCustomAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int i)
    {
        ImageViewURLFragment fragment = new ImageViewURLFragment();
        fragment.setImageURL(_urls.get(i));

        return fragment;
    }

    public String getItemUrl(int i)
    {
        return _urls.get(i);
    }

    @Override
    public int getCount() {
        return _urls.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }

    public void addImageURL(String url)
    {
        _urls.add(url);
    }
}

And this is the main activity, notice the comment with the get:

public class MainActivity extends FragmentActivity
{
    ImageViewURLCustomAdapter _iva=null;
    ViewPager _vp=null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        _iva = new ImageViewURLCustomAdapter(
                        getSupportFragmentManager());
        _iva.addImageURL("http://upload.wikimedia.org/wikipedia/commons/b/b4/Saturn_(planet)_large.jpg");
        _iva.addImageURL("http://planetthreesixty.com/sites/default/files/planet.jpg");

        _vp = (ViewPager) findViewById(R.id.viewpager);
        _vp.setAdapter(_iva);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if(item.getItemId()==R.id.saveToSD)
        {
            int index= _vp.getCurrentItem();
            Fragment fragment=_iva.getItem(index);

            //THis is where I need to get fragment view, but always returns null
                    View view= fragment.getView();
            ImageView iv=(ImageView)view.findViewWithTag("imageView");
            Bitmap bmp=iv.getDrawingCache();


        }


        return super.onOptionsItemSelected(item);
    }
}

Sorry for the long sources, I have cleaned everything I could, but I wanted to present all playing parts. Could anybody guess what is hapenning?

Thanks in advance.

解决方案

That is happening because you're expecting _iva.getItem(index); to return the Fragment that the ViewPager uses for the page corresponding to the specified index. That will not happen as the ViewPager has already called the getItem method to get the fragments it needs and after your call the getItem method you get a new ImageViewURLFragment instance. This new instance isn't tied to the Activity(getActivity() returns null) and its view wasn't created.

As you use a FragmentStatePagerAdapter try the code below to get the currently visible Fragment:

if (item.getItemId()==R.id.saveToSD) {
     int index = _vp.getCurrentItem();
     Fragment fragment = _vp.getAdapter().instantiateItem(_vp, index);
     //...

这篇关于片段getView()总是返回null由一个FragmentStatePagerAdapter创建片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 15:30