Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Fragments Introducing Fragments Adding a Fragment

pawkan kenluecha
pawkan kenluecha
26,303 Points

I got a error, they said can't find symbol of ListFragment.

Could someone help me check this code ? I'm not sure what was the problem. I try to check this with the lesson and I don't seem anything weird.

NewsFragment.java
public class NewsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      View view = inflater.inflate(R.layout.fragment_news, container, false);

        return view;
    }
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      ListFragment fragment = new ListFragment();
      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.add(R.id.placeholder, fragment);
      fragmentTransaction.commit();

    }
}

3 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Pawkan,

I know this is an old post, and you may have it solved already. Your code is correct, except for a naming error. It's NewsFragment not ListFragment. So just change

ListFragment fragment = new ListFragment();

to

NewsFragment fragment = new NewsFragment();

Keep Coding! :) :dizzy:

pawkan kenluecha
pawkan kenluecha
26,303 Points

Oh, thanks. But, is it possible to explain more detail ? why is it "NewsFragment" instead of "ListFragment" ? I tried to google this "NewsFragment" function but I found nothing.

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Sure pawkan kenluecha

It's really only because of this challenge. This particular challenge has a Class named NewsFragment.java which extents the Fragment class from Java. So, by calling NewsFragment, you are really just using the Class that is unique to this program. If it was called "PaperClass", then thats what we'd use.

To put it into perspective. In the video, we use ListFragment, because that is the name Ben gave his class ... ListFragment.java, which also extends the Fragment Class.

So, in this case, Google won't help. You could Google "Fragment Class Java" and read about the Fragment class. I hope that helps clearing it up. :) :dizzy:

pawkan kenluecha
pawkan kenluecha
26,303 Points

I see, I got it. Thank you for clarification.

Hi there,

At the top of MainActivity have you imported ListFragment?

Steve.

pawkan kenluecha
pawkan kenluecha
26,303 Points

I got this error after I imported ListFragment.

./NewsFragment.java:1: error: cannot find symbol import android.app.ListFragment; ^ symbol: class ListFragment location: package android.app 1 error

I put "import android.app.ListFragment;" on the top.

Apologies for the late respose; I was away with work.

Have you solved this yet?

If not, try using Android Studio's 'organise imports' option within the menus. Comment out your lines of code in the class then try adding a ListFragment line again - the organise imports should then pull in the correct code you need.

I don't think you need the app.android bit but A.S. should sort this all for you

Steve/