Cannot resolve method getActivity()
Asked Answered
G

4

27
  • I am new to Android and learning to create fragments in Android by following this
    example: Fragment Navigation Drawer

  • The 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 file

  • Is 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
Greenroom answered 11/9, 2015 at 15:38 Comment(1)
getActivity() is called generally from within a fragment.Vinna
B
79

You can use:

this Or `MainActivity.this`

Instead of:

getActivity()
Buskined answered 19/5, 2016 at 12:37 Comment(0)
I
31

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.

Ingold answered 11/9, 2015 at 15:45 Comment(9)
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 viewGreenroom
// 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. SteinGreenroom
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
T
1

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 so View.getContext();
  • Get App-level context : getApplicationContext() this method returns the activity that houses the entire life cycle of the application.
Towney answered 31/3, 2021 at 18:41 Comment(0)
R
0

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);
}
Rumery answered 19/5, 2016 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.