Yuu
Yuu

Reputation: 619

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;
}
}

Upvotes: 0

Views: 3758

Answers (4)

asapsonter
asapsonter

Reputation: 41

you can also use

getActivity().getSharedPreferences()

Upvotes: 0

Manikanta
Manikanta

Reputation: 3421

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

Upvotes: 1

curiousMind
curiousMind

Reputation: 2812

you have to write this into the your method

@SuppressLint("NewApi")
public Connection CONN(Context context) {
SharedPreferences pref = context.getSharedPreferences("MyPrefs",Context.MODE_PRIVATE);

}

or you can write it into constuctor also

Upvotes: 0

Simon Tenbeitel
Simon Tenbeitel

Reputation: 877

You need to be in a Context, or have a variable containing Context to call this method.

public class ConnectionClass {
    Context context;
    SharedPreferences pref;
    public ConnectionClass(Context context) {
        this.context = context;
        pref = context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
    }

Upvotes: 1

Related Questions