Android Fragment no view found for ID?

2018-12-31 17:32发布

I have a fragment I am trying to add into a view.

FragmentManager fragMgr=getSupportFragmentManager();
feed_parser_activity content = (feed_parser_activity)fragMgr
                                    .findFragmentById(R.id.feedContentContainer);
FragmentTransaction xaction=fragMgr.beginTransaction();

if (content == null || content.isRemoving()) {
    content=new feed_parser_activity(item.getLink().toString());
    xaction
        .add(R.id.feedContentContainer, content)
        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        .addToBackStack(null)
        .commit();
    Log.e("Abstract", "DONE");
}

When this code is executed I get the following error in debug..

java.lang.IllegalArgumentException: No view found for id 0x7f080011 
   for fragment feed_parser_activity{41882f50 #2 id=0x7f080011}

feed_parser_activity is a Fragment that is set to Fragment layout in xml.
I am using a FragmentActivity to host the Fragment Layout holding the feed_parser_layout.
Am I coding this correctly above?

30条回答
栀子花@的思念
2楼-- · 2018-12-31 18:08

I had this problem (when building my UI in code) and it was caused by my ViewPager (that showed Fragments) not having an ID set, so I simply used pager.setID(id) and then it worked.

This page helped me figure that out.

查看更多
君临天下
3楼-- · 2018-12-31 18:08

Inside your fragment use this:

int id = this.getId();
查看更多
浪荡孟婆
4楼-- · 2018-12-31 18:09

Another scenario I have met. If you use nested fragments, say a ViewPager in a Fragment with it's pages also Fragments.

When you do Fragment transaction in the inner fragment(page of ViewPager), you will need

FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

getActivity() is the key here. ...

查看更多
泛滥B
5楼-- · 2018-12-31 18:09

My mistake was on the FragamentTransaction.

I was doing this t.replace(R.layout.mylayout); instead of t.replace(R.id.mylayout);

The difference is that one is the layout and the other is a reference to the layout(id)

查看更多
听够珍惜
6楼-- · 2018-12-31 18:10

With nested fragments

By using getChildFragmentManager() instead of getActivity.getSupportFragmentManager() resolved crash java.lang.IllegalArgumentException: No view found for id for me.

查看更多
梦醉为红颜
7楼-- · 2018-12-31 18:11

In my case I was trying to show a DialogFragment containing a pager and this exception was thrown when the FragmentPagerAdapter attempted to add the Fragments to the pager. Based on howettl answer I guess that it was due to the Pager parent was not the view set in setContentView() in my FragmentActivity.

The only change I did to solve the problem was to create the FragmentPagerAdapter passing in a FragmentMager obtained by calling getChildFragmentManager(), not the one obtained by calling getFragmentManager() as I normally do.

public class PagerDialog extends DialogFragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.pager_dialog, container, false);

    MyPagerAdapter pagerAdapter = new MyPagerAdapter(getChildFragmentManager());
    ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager);
    pagerAdapter.setAdapter(pagerAdapter);

    return rootView;
}

}

查看更多
登录 后发表回答