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 trialpawkan kenluecha
26,303 PointsI 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.
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;
}
}
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
Treehouse Moderator 145,860 PointsHey 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! :)
Steve Hunter
57,712 PointsHi there,
At the top of MainActivity
have you imported ListFragment
?
Steve.
pawkan kenluecha
26,303 PointsI 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.
Steve Hunter
57,712 PointsApologies 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/
pawkan kenluecha
26,303 Pointspawkan kenluecha
26,303 PointsOh, 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
Treehouse Moderator 145,860 PointsJason Anders
Treehouse Moderator 145,860 PointsSure 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. :)
pawkan kenluecha
26,303 Pointspawkan kenluecha
26,303 PointsI see, I got it. Thank you for clarification.
Jason Anders
Treehouse Moderator 145,860 PointsJason Anders
Treehouse Moderator 145,860 PointsYou're very Welcome!