Search code examples
javaandroidsharedpreferences

Android : Getting error "cannot resolve getSharedPreferences(java.lang.String, int)"


I'm trying to access the SharedPreferences in a non-activity class but I always get this error cannot resolve getSharedPreferences(java.lang.String, int) . But when I use the getSharedPreferences in normal activity class I don't get this error.

Does anyone know how to fix this?

Here's my code :

public class ConnectionClass {

SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
String ip = pref.getString("serverIP", "");

// String ip = "";
// String ip = server_IP;
String classDriver = "net.sourceforge.jtds.jdbc.Driver";
String db = "passoDB";
String un = "sa";
String password = "dhen1234";

@SuppressLint("NewApi")
public Connection CONN(Context context) {

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection conn = null;
    String ConnURL = null;
    try {
        Class.forName(classDriver);
        ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
                + "databaseName=" + db + ";user=" + un + ";password="
                + password + ";";
        conn = DriverManager.getConnection(ConnURL);
    } catch (SQLException se) {
        Log.e("ERRO", se.getMessage());
    } catch (ClassNotFoundException e) {
        Log.e("ERRO", e.getMessage());
    } catch (Exception e) {
        Log.e("ERRO", e.getMessage());
    }
    return conn;
}
}

Solution

  • yes as simon said for accessing shared preferences you need context so in order to make that code work i will add this line.

        public class ConnectionClass {
    
    
    String classDriver = "net.sourceforge.jtds.jdbc.Driver";
    String db = "passoDB";
    String un = "sa";
    String password = "dhen1234";
    
    
     String ip = "";
    
    
     public ConnectionClass(Context context){
            SharedPreferences prefs = context.getSharedPreferences("MyPrefs", context.MODE_PRIVATE);
            ip = prefs.getString("serverIP", "");
    
     }
    
    
    @SuppressLint("NewApi")
    public Connection CONN(Context context) {
    
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection conn = null;
        String ConnURL = null;
        try {
            Class.forName(classDriver);
            ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
                    + "databaseName=" + db + ";user=" + un + ";password="
                    + password + ";";
            conn = DriverManager.getConnection(ConnURL);
        } catch (SQLException se) {
            Log.e("ERRO", se.getMessage());
        } catch (ClassNotFoundException e) {
            Log.e("ERRO", e.getMessage());
        } catch (Exception e) {
            Log.e("ERRO", e.getMessage());
        }
        return conn;
    }
    }
    

    I have not indented it properly, i hope it helps.

    While Calling the class you should call this way:

    ConnectionClass connectionClass = new ConnectionClass(YourActivity.this);//if in activity
    
    ConnectionClass connectionClass = new ConnectionClass(getActivity());//if in fragment