-1

I'm trying to set up a log in system for an app, and I've been following along with this video series. Basically I understand the general concept of what's going on, but not why he's doing what he's doing sometimes. Anyways, I get some errors with the lines that say

final FragmentManager fm = getActivity().getSupportFragmentManager();
fm.beginTransaction().replace(R.id.content, new LoginFragment()).commit();

The errors say

Can't resolve method 'getActivity()'

on that first line, even though I've seen it in the documentation, and

Wrong 2nd argument type. Found: 'your.project.structure.LoginFragment', required: 'android.support.v4.app.Fragment'

on the second line where I have new LoginFragment()

After hours of googling, trying things and reading documentation, I still have no idea what I'm doing, and I end up going in circles. I also have no idea if I'm even giving enough information, so ask away and I'll do my best to respond accurately.

PaulBunion
  • 346
  • 2
  • 18

2 Answers2

0

Your LoginFragment doesn't override Fragment class. This is the problem of the second error.

For the first one, is the code you posted in an Activity? Or is it in a Fragment?

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
  • I'm not sure how to make the `LoginFragment` class override the `Fragment` class. I've overridden methods before, but not a whole class (`LoginFragment` does extend `FragmentActivity` btw, if that's important). Currently it's in a `Fragment`. I do have a branch of this project where everything is an activity, and I get the same error (except for the `new LoginFragment()` error, it gives the same unresolvable message as the `getActivity()` one). – PaulBunion Apr 16 '19 at 17:45
  • If it extends `FragmentActivity` it's not a `Fragment`, it's an `Activity`. Change the implementation to `Fragment`. If the code is in an `Activity` you don't have to call `getActivity()` as you are in an `Activity` just remove it or go with `this.` – Luca Nicoletti Apr 16 '19 at 17:52
0
  • LoginFragment() extend Fragment.
  • If the code is in an Activity, use this.getSupportFragmentManager(). If in Fragment , use getFragmentManager().
Hải Bùi
  • 39
  • 1
  • 4
  • I tried doing that as per the suggestion from @Luca Nicoletti, but then it says that `getSupportFragmentManager()` cannot be resolved. – PaulBunion Apr 17 '19 at 15:00