1

I have following code :

  Context context = Activity.getApplicationContext();
             SharedPreferences settings = context.getSharedPreferences("AutoMsgSharedPrefs", MODE_PRIVATE);

            // Writing data to SharedPreferences
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("key", "some value");
            editor.commit();

I have been trying to use SharedPrefs to store messages given in - "Conversation" class as in sample - https://developer.android.com/samples/MessagingService/index.html. But, I get "can not reference non-static method from a static class if I try to achieve it in constructor of "Conversation" class. So How do I resolve this?

Here is the screenshot of error if I update as suggested :

enter image description here

Smitha
  • 6,110
  • 24
  • 90
  • 161

4 Answers4

1

To get the context no need to use Activity class. Change this code

Context context = Activity.getApplicationContext();

to

Context context = getApplicationContext();
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
1

Explanation: Activity class does not have a static method getApplicationContext(), because this method is non static, so you need to have an object instance. So call this method on Activity on Context instance.

1

Here

Context context = Activity.getApplicationContext();

This line not return a valid Context for your application to call getSharedPreferences.

To call getSharedPreferences from non Activity,Service,... classes you should need to pass valid context from application component like from Activity,Service,..

To get Context in Conversation use Conversation class constructor which is already created in given example you will need to add one more parameter:

Context mContext;
public Conversation(int conversationId, String participantName,
                            List<String> messages,Context mContext) {
            .....
            this.mContext=mContext;
        }

Now use mContext to call getSharedPreferences method from Conversation class :

SharedPreferences settings = mContext.getSharedPreferences("AutoMsgSharedPrefs", 
                                                           Context.MODE_PRIVATE);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

As @ρяσѕρєя K already pointed out you have to somehow grant your non-Context class access to your Context instance. For example through introducing a new parameter.

public Conversation(int conversationId, String participantName,
                            List<String> messages, Context context) {
            .....

        }

But keep in mind:

It is discouraged to save references to long-life and heavy weight components like Contexts in your classes because this strong reference will exclude the context from the garbage collection and thus causing memory leaks.

So instead of storing your Context you can use it to initialize your Conversation object as you like and let the scope of the constructor take care of discarding the short-term reference to your Context. If you should need a Context multiple times though, you could write a method which takes a Context instance as a parameter and call it to do the dirty work:

public void doStuff(Context context) {
          // do your work here
}
Community
  • 1
  • 1