-2

I am converting an activity to a fragment so that I can use it within the Android Studio Navigation Drawer template.

I have changed all my activities to extend from fragment, and ensured that I am importing v4 support for fragments.

I have resolved most of the issues that Android Studio has raised...but I am really stuck on the final issue:

I have a fragment that used to be the mainActivity (now called liveview_fragment.java), which calls a class called ApplicationController in a file called ApplicationController.java. However, the line in liveview_fragment that calls ApplicationController gives the following error:

screenshot of error

I am new to android/java, with most of my learning coming from tutorials and trial/error. Please could someone point out what I have missed in order to resolve this issue?

Thanks

3 Answers3

0

You'll probably need to pass a context to the ApplicationController. Try with getActivity() or getContext() instead of this

quiro
  • 357
  • 1
  • 8
0

The problem is that liveview_fragment is now a Fragment instead of an Activity. In order to get the hosting Activity, you can call getActivity(). This will probably fix the error.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

Always use the getActivity() method to get the context of your attached activity, but always remember one thing: Fragments are slightly unstable and getActivity returns null some times, so for that, always check the isAdded() method of fragment before getting context by getActivity() refer Using context in a fragment

Community
  • 1
  • 1
Pallav Grover
  • 55
  • 2
  • 8