• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

1 Error needs fix - cannot make a static reference to the non static method

 
John A B Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

(I've amended my question because I have made some progress)

This is my first post and first venture onto a forum about Android Development.

I've been working on this small android app for the last week and half and have done about as much as possible without anyone's help so far via online tutorials.

I have a specific issue now which I have not been able to resolve and I've been reading and trying different things to get it working since yesterday.

I have a main activity called GuitarActivity.java and its layout activity_guitar.xml.

There is a row of buttons which pull in a fragment called guitar_tune_01.xml

The fragment has a row of buttons which need to play a different sound each.

I have implemented the fragment code into the main activity GuitarActivity.java

I am getting one last error which I really need someone to help me fix.


Error is on line 38 and all recurring instances: MediaPlayer mp = MediaPlayer.create(getApplicationContext(),

Error says

rename in file (ctrl+2, r)

and

cannot make a static reference to the non static method getapplicationcontext from the type contextwrapper

MyFragment1 is a static class, and I can't reference the non-static method from there. I need to move the fragment to the outer scope (its own class file) and remove the static modifier on the class declaration, and that should do it.

This was from someone who was trying to help me but is over my head.

Please help.

Many thanks, Jo

GuitarActivity.java



activity_guitar.xml



fragment_guitar_tune_01.xml

 
John A B Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have amended the question because I have made some progress. Please help.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you were given for the error is correct. The fragments are static inner classes, they are in a scope in which an instance of the Activity does not exist. You then have a method which you call which can only be referenced from an instance of the Activity. Since you don't have an instance, you can't make that call. For this situation, there are two basic fixes.

1) Remove the static keyword from the Fragment class definition. This would tie the definition of the class, and the instances of the class, to an instance of the the Activity and you would gain access to the Activity's instance methods.

2) Call getActivity().getApplicationContext() instead. The method getActivity() comes from the Fragment class and gives you access to the methods of the Activity. If you need a specific method that you added to the Activity, then you would need to cast: ((MyActivity)getActivity()).myMethod();

The second approach is probably the better one. The Fragment lifecycle is independent of the Activity lifecycle, and by making the Fragment an instance-level inner class you would end up tieing the two together. The FragmentManager may end up not liking it, and there could be issues when you do things like rotatge the screen.
 
John A B Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The first fix is working. I'm am sure I tried that the other day and it still didn't work.

At least this first fix will ensure my app is presentable to my module tutor and does what it says on the tin and will allow me to continue implementing the rest.

Would it be possible for you to provide a code example of your second suggestion?

I'm a week into learning the Java language from scratch separate to this project. But this project has to be finished in the next day or two and I'm not nearly competent enough to understand what you mean in the second fix.


Many thanks
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is written exactly in my previous post. Find the locations where you call getApplicationContext() and replace it with getActivity().getApplicationContext()
 
John A B Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am on it. Cheers.
 
John A B Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. Working now. 2 days spent trying to fix that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic