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
Akki
  • 1,221
  • 3
  • 14
  • 33

4 Answers4

79

You can use:

this Or `MainActivity.this`

Instead of:

getActivity()
ADM
  • 20,406
  • 11
  • 52
  • 83
zohreh
  • 913
  • 1
  • 7
  • 9
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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 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. – Akki Sep 11 '15 at 15:48
  • Could you post some relevant code, rather than describing it? i.e.: the Main Activity onCreate() method? – Phantômaxx Sep 11 '15 at 15:51
  • @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 – Akki Sep 11 '15 at 15:53
  • // 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); } – Akki Sep 11 '15 at 15:53
  • Sorry the comment didn't allowed the code to paste. You will have to paste it on notepad to read it. – Akki Sep 11 '15 at 15:54
  • Did I made a bluner... Sorry! @Frank N. Stein – Akki Sep 11 '15 at 16:22
  • Well, yes. And the onCreateView of the Fragment would also be useful. – Phantômaxx Sep 11 '15 at 16:24
  • So, where do you initialize the Fragment? – Phantômaxx Sep 11 '15 at 16:28
  • Let me suggest you to follow this example: http://developer.android.com/intl/es/training/implementing-navigation/nav-drawer.html. There's also some downloadable code. – Phantômaxx Sep 11 '15 at 16:41
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.
Marcel
  • 139
  • 2
  • 6
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);
}
Majid Roustaei
  • 1,556
  • 1
  • 20
  • 39
Tejas Parmar
  • 79
  • 1
  • 2