0

I am trying to display a new fragment in Android when a user taps on an item in a ListView. Here is my code for doing so:

@Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            final FragmentManager fm = getFragmentManager();
            final FragmentTransaction ft = fm.beginTransaction();
            DetailFragment df = new DetailFragment();

            Toast.makeText(getActivity().getBaseContext(), "Clicked with R.id.list=" + R.id.list, Toast.LENGTH_LONG).show();    

            ft.replace( R.layout.fragment_favorites, df, "DetailFragment" ).addToBackStack(null).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
        }

When I run this code and tap on an item in the list view, I get the exception:

10-09 22:39:05.336: E/AndroidRuntime(25157): java.lang.IllegalArgumentException: No view found for id 0x7f030029 (com.example.proj1:layout/fragment_favorites) for fragment DetailFragment{42430970 #3 id=0x7f030029 DetailFragment}
6
  • 2
    Replace R.layout.fragment_favorites with the id of your fragment container, ft.replace(R.id.frag_container) Oct 10, 2014 at 3:05
  • 1
    Your R.java might be corrupt. That might be why the view id cannot be found. Try to clean build your project(In Eclipse, go to Project -> Clean ) and then run it.
    – faizal
    Oct 10, 2014 at 3:15
  • add your activity layout file
    – Sayem
    Oct 10, 2014 at 3:16
  • Thanks for the suggestion, @Law Gimenez. I am still having issues getting the view to display (I've updated the original post with the logic for this code). Do you have any further suggestions for this? Oct 10, 2014 at 3:52
  • Did you changed ID as mentioned by Law Gimenz within onItemClickListener. Oct 10, 2014 at 3:56

1 Answer 1

0

ft.replace(int containerViewId, Fragment fragment, String tag) Replace an existing fragment that was added to a container. containerViewId is Identifier of the container whose fragment(s) are to be replaced. It is not layout.

Check in your activity layout xml, to find the id of the fragment

replace

ft.replace( R.layout.fragment_favorites, df, "DetailFragment" ).addToBackStack(null).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();

with

ft.replace("Your fragment id", df, "DetailFragment").commit();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.