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
Cannot resolve method getActivity()
Asked Answered
getActivity() is called generally from within a fragment. –
Vinna
You can use:
this Or `MainActivity.this`
Instead of:
getActivity()
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.
Yes, I analyzed it and then made the changes to my code and now getting NullPointerException error. Am i doing the things in right way. I am new to android and its my first example to trying out android. –
Greenroom
Could you post some relevant code, rather than describing it? i.e.: the Main Activity onCreate() method? –
Helyn
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Set a Toolbar to replace the ActionBar. toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); NavigationView nvDrawer = (NavigationView) findViewById(R.id.nvView); // Setup drawer view setupDrawerContent(nvDrawer); // Find our drawer view –
Greenroom
// Set the menu icon instead of the launcher icon. final ActionBar ab = getSupportActionBar(); ab.setHomeAsUpIndicator(R.drawable.ic_menu); ab.setDisplayHomeAsUpEnabled(true); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Find our drawer view dlDrawer= (DrawerLayout) findViewById(R.id.drawer_layout); drawerToggle = setupDrawerToggle(); // Tie DrawerLayout events to the ActionBarToggle dlDrawer.setDrawerListener(drawerToggle); } –
Greenroom
Sorry the comment didn't allowed the code to paste. You will have to paste it on notepad to read it. –
Greenroom
Did I made a bluner... Sorry! @Frank N. Stein –
Greenroom
Well, yes. And the onCreateView of the Fragment would also be useful. –
Helyn
So, where do you initialize the Fragment? –
Helyn
Let me suggest you to follow this example: developer.android.com/intl/es/training/implementing-navigation/…. There's also some downloadable code. –
Helyn
It has been clearly pointed out that you cannot use the getActivity() method in an activity. Well, other alternatives apart from the this
keyword could be;
- Get current activity context : using the getContext()
This method can be called on a View like text view like so
textView.getContext();
This will give the context of the activity in which the view is currently hosted in. i.e something like soView.getContext();
- Get App-level context : getApplicationContext() this method returns the activity that houses the entire life cycle of the application.
In Fragment it is best to use onAttach()
method to get the instance of an Activity
attached to it.
here is a sample code:
@Override
public void onAttach (Activity activity) {
super.onAttach(activity);
}
© 2022 - 2024 — McMap. All rights reserved.