Android:初始化变量为已设偏好时遇非静态方法静态上下文引用错误求助
Hey there! Let's break down this error and get your code working right away.
What the Error Means
That error message is telling you two key things:
- You're trying to call a non-static method (a method that belongs to an instance of a class, not the class itself)
- You're doing this from a static context (like a static variable declaration, static method, or static initializer block)
Static code belongs to the class as a whole, not to any specific object instance of that class. Non-static methods need an actual object instance to run—so you can't call them without first creating or accessing that instance.
Why Your Code Is Triggering This
Your line private int mShuffleMode = SharedPreferences.getInt("shufflemode"); has two issues:
getInt()is not a static method of theSharedPreferencesclass. It's a method that belongs to a specific instance ofSharedPreferences(you have to create or retrieve that instance first).- If you're declaring
mShuffleModeas a static variable (or initializing it in a static block), you're working in a static context where you can't access non-static methods like the instance-basedgetInt().
Step-by-Step Fix
Here's how to fix this properly:
1. First, Get a SharedPreferences Instance
You can't call getInt() directly on the SharedPreferences class. Instead, you need to retrieve an instance using your app's Context:
- In an Activity or Fragment: Use
getSharedPreferences()(specify your preferences file name and mode) - In other classes: Pass a
Context(use the Application Context to avoid memory leaks)
2. Initialize Your Variable in a Non-Static Context
Avoid initializing variables that depend on Context (like SharedPreferences) in static declarations. Instead, initialize them in a lifecycle method (like onCreate() for Activities) or an instance method.
Example Working Code
Here's how it should look in an Activity:
// Declare your variable (non-static, unless you have a specific reason to make it static) private int mShuffleMode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_activity_layout); // Get the SharedPreferences instance SharedPreferences prefs = getSharedPreferences("your_preferences_file_name", MODE_PRIVATE); // Retrieve the value—note the second parameter is a default value (required!) mShuffleMode = prefs.getInt("shufflemode", 0); // Use 0 or your preferred default mode }
If You Need Static Access (Use Carefully!)
If you absolutely need to get this value from a static method, pass a Context (use Application Context to prevent leaks):
public static int getShuffleMode(Context context) { SharedPreferences prefs = context.getApplicationContext() .getSharedPreferences("your_preferences_file_name", MODE_PRIVATE); return prefs.getInt("shufflemode", 0); }
Key Notes
- Always include a default value in
getInt()—this ensures your app doesn't crash if the "shufflemode" key doesn't exist yet. - Avoid storing
Contextreferences in static fields—it's a common cause of memory leaks. Use the Application Context if you need static access.
内容的提问来源于stack exchange,提问作者Brandon Nolet

