Error:non static method 'edit' cannot be referenced in static context

Asad Khan
public class NavigationDrawerFragment extends Fragment {

    public static final String PREF_FILE_NAME="testpref";
    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLyout;

    private boolean mUserLearnedDrawer;
    private boolean mFromSavedInstanceState;
    public NavigationDrawerFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
}


public void setUp(DrawerLayout drawerLayout,Toolbar toolbar) {
    mDrawerLyout=drawerLayout;
    mDrawerToggle=new ActionBarDrawerToggle(getActivity(),drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }
    };
    mDrawerLyout.setDrawerListener(mDrawerToggle);

}
 public void saveToPreferences(Context context, String preferenceName, String preferenceValue)
 {
    SharedPreferences sharedPreferences =  context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor=SharedPreferences.edit();
    editor.putString(preferenceName, preferenceValue);
    editor.commit();
}

}

So i am having error on the line, SharedPreferences.Editor editor=SharedPreferences.edit();

Error:non static method 'edit' cannot be referenced in static context

Rajesh

Try

SharedPreferences.Editor editor=sharedPreferences.edit();

instead of

SharedPreferences.Editor editor=SharedPreferences.edit();

You can not directly SharedPreferences.edit();.

You have to create object of SharedPreferences and You have created which is sharedPreferences so use it to call edit() method.

I hope it helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"Non-static method cannot be referenced from static context" error

From Dev

Non-static edit() cannot be referenced from a static context

From Java

non static method cannot be referenced from a static context

From Dev

non-static method getIntent() cannot be referenced from a static context

From Dev

Non-static method getSocketFactory cannot be referenced from a static context

From Java

Error: Non-static method 'findViewById(int)' cannot be referenced from a static context

From Dev

setDisplayHomeAsUpEnabled() error: non-static method cannot be referenced from a static context

From Dev

error: non-static method setListMovie(ArrayList<Movie>) cannot be referenced from a static context

From Java

getting Error: non-static method 'getResources()' cannot be referenced from a static context

From Dev

error: non-static method unbind() cannot be referenced from a static context

From Dev

error: non-static method getActivity() cannot be referenced from a static context

From Dev

How to resolve this error?:non-static method maxOf(int,int) cannot be referenced from a static context

From Java

i am getting a error in animate() which is "error: non-static method animate() cannot be referenced from a static context"

From Dev

Error: Non-static variable super cannot be referenced from a static context >>but i use static keyword

From Dev

Trying to get my show-method to work (Non static method cannot be referenced from static context)

From Dev

Map.merge .. non-static method cannot be referenced from static context

From Dev

non-static method matcher(CharSequence) cannot be referenced from a static context

From Java

Why does String::isEmpty works when non-static method cannot be referenced from a static context?

From Java

java - Non-static method 'getLogger' cannot be referenced from a static context

From Dev

non-static method count(int) cannot be referenced from a static context

From Dev

Non-static method cannot be referenced from a static context when flattening map java 8

From Java

Non static field cannot be referenced from a static context- Main method

From Java

Non-static method cannot be referenced from a static context in java 8 streams

From Java

Non-static method cannot be referenced from a static context in java 8 streams

From Dev

Flutter: non-static method registerWith(Registrar) cannot be referenced from a static context

From Java

Android: Non-static method cannot be referenced from static context. Confused?

From Java

What is the reason behind "non-static method cannot be referenced from a static context"?

From Dev

Non-static method display(Main) cannot be referenced from a static context

From Dev

JAVA - non-static method add(E) cannot be referenced from a static context

Related Related

  1. 1

    "Non-static method cannot be referenced from static context" error

  2. 2

    Non-static edit() cannot be referenced from a static context

  3. 3

    non static method cannot be referenced from a static context

  4. 4

    non-static method getIntent() cannot be referenced from a static context

  5. 5

    Non-static method getSocketFactory cannot be referenced from a static context

  6. 6

    Error: Non-static method 'findViewById(int)' cannot be referenced from a static context

  7. 7

    setDisplayHomeAsUpEnabled() error: non-static method cannot be referenced from a static context

  8. 8

    error: non-static method setListMovie(ArrayList<Movie>) cannot be referenced from a static context

  9. 9

    getting Error: non-static method 'getResources()' cannot be referenced from a static context

  10. 10

    error: non-static method unbind() cannot be referenced from a static context

  11. 11

    error: non-static method getActivity() cannot be referenced from a static context

  12. 12

    How to resolve this error?:non-static method maxOf(int,int) cannot be referenced from a static context

  13. 13

    i am getting a error in animate() which is "error: non-static method animate() cannot be referenced from a static context"

  14. 14

    Error: Non-static variable super cannot be referenced from a static context >>but i use static keyword

  15. 15

    Trying to get my show-method to work (Non static method cannot be referenced from static context)

  16. 16

    Map.merge .. non-static method cannot be referenced from static context

  17. 17

    non-static method matcher(CharSequence) cannot be referenced from a static context

  18. 18

    Why does String::isEmpty works when non-static method cannot be referenced from a static context?

  19. 19

    java - Non-static method 'getLogger' cannot be referenced from a static context

  20. 20

    non-static method count(int) cannot be referenced from a static context

  21. 21

    Non-static method cannot be referenced from a static context when flattening map java 8

  22. 22

    Non static field cannot be referenced from a static context- Main method

  23. 23

    Non-static method cannot be referenced from a static context in java 8 streams

  24. 24

    Non-static method cannot be referenced from a static context in java 8 streams

  25. 25

    Flutter: non-static method registerWith(Registrar) cannot be referenced from a static context

  26. 26

    Android: Non-static method cannot be referenced from static context. Confused?

  27. 27

    What is the reason behind "non-static method cannot be referenced from a static context"?

  28. 28

    Non-static method display(Main) cannot be referenced from a static context

  29. 29

    JAVA - non-static method add(E) cannot be referenced from a static context

HotTag

Archive