2

I'm trying to create and run the fragment example shown at http://developer.android.com/guide/components/fragment.html

I've read the following link Android Compatibility Package and have looked in the googlesource repository.

In showDetails() method in the TitlesFragment.java class there is a line ft.replace(R.id.a_item); // ft.replace(int); But I have the following error

a_item cannot be resolved or is not a field

How should this resource be defined?

Thanks.

Community
  • 1
  • 1

3 Answers3

2

Perhaps, it's a mistake. The answer is here https://code.google.com/p/android/issues/detail?id=54837

The author of linked message advises to replace code fragment

if (index == 0) {
    ft.replace(R.id.details, details);
} else {
    ft.replace(R.id.a_item, details);
}

with code fragment

if (index == 0) {
    ft.add(R.id.details, details);
} else {
    ft.replace(R.id.details, details);
}

or maybe just

ft.replace(R.id.details, details);
0

This id is supposed to be the id of a Fragment since it occurs inside FragmentTransaction.replace(). You should define the id either:

  • Inside a layout file that declares your fragment : <fragment class="...." id="@+id/a_item" />
  • Inside a resource file using <item name="a_item" type="id" />
  • Or replace an a_item inside your code by a custom int that is a constant you define
Community
  • 1
  • 1
Antoine Marques
  • 1,379
  • 1
  • 8
  • 16
0

Please note that most of those examples are outdated and have many mistakes. If you notice on this same example they used findFragmentbyID using R.id.details, yet there are no fragments with that id, it is the FrameLayout that has that id. Also be aware that the API used in this example is deprecated.

with that said, this is what the showDetails method should look like:

void showDetails(int index) {

        mCurCheckPosition = index;

        if (mDualPane) {
            // We can display everything in-place with fragments, so update
            // the list to highlight the selected item and show the data.
            getListView().setItemChecked(index, true);


            // Make new fragment to show this selection.
            DetailsFragment details = DetailsFragment.newInstance(index);

            // Execute a transaction, replacing any existing fragment
            // with this one inside the frame.
            FragmentTransaction ft = getFragmentManager().beginTransaction();

            ft.replace(R.id.details, details);                               
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();

        } else {
            // Otherwise we need to launch a new activity to display
            // the dialog fragment with selected text.
            Intent intent = new Intent();
            intent.setClass(getActivity(), DetailsActivity.class);
            intent.putExtra("index", index);
            startActivity(intent);
        }
    }

A few extra lines of code could be added so that clicking on an already selected item does nothing rather than recreate and replace the same fragment with the same information.

Osagui Aghedo
  • 330
  • 4
  • 12