You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android:初始化变量为已设偏好时遇非静态方法静态上下文引用错误求助

Fixing "Non-static method can't be referenced from a static context" in Your SharedPreferences Code

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:

  1. You're trying to call a non-static method (a method that belongs to an instance of a class, not the class itself)
  2. 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:

  1. getInt() is not a static method of the SharedPreferences class. It's a method that belongs to a specific instance of SharedPreferences (you have to create or retrieve that instance first).
  2. If you're declaring mShuffleMode as 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-based getInt().

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 Context references in static fields—it's a common cause of memory leaks. Use the Application Context if you need static access.

内容的提问来源于stack exchange,提问作者Brandon Nolet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:10:36