I am new to Android and learning to create fragments in Android by following this
example: Fragment Navigation DrawerThe code between Navigating between Menu Items and Add Navigation Header consists a method
getActivity()
.As the author didn't mentioned where to paste this code, I pasted in my
MainActivity.java
fileIs code between Navigating between Menu Items and Add Navigation Header pasted at correct location by me?
In method
selectDrawerItem(MenuItem menuItem)
there is a comment// Create a new fragment and specify the planet to show based on position
Does author expects me to add something over here.- The project files layout created by me on AndroidStudio is as follow:AndroidStudio Snapshot
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
An Activity has no
getActivity()
method.Fragments have.
Because
getActivity()
says: "return the Activity which contains me".And while Framents are contained in Activities, Activities themselves aren't.
You can use:
Instead of:
In Fragment it is best to use onAttach() method to get the instance of an Activity attached to it.