Android:获取错误“无法解析getSharedPreferences(java.lang.String,int)”

时间:2015-11-25 07:48:35

标签: java android sharedpreferences

我正在尝试访问非活动类中的SharedPreferences但我总是收到此错误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; } } 。但是当我在普通活动类中使用getSharedPreferences时,我没有收到此错误。

有谁知道如何解决这个问题?

这是我的代码:

$routeProvider
        .when('/numbers/:country',{
            templateUrl : '/foo/bar/baz/numbers.html',
            controller : 'NumbersController'
        })

3 个答案:

答案 0 :(得分:1)

您需要位于Context,或者拥有包含Context的变量才能调用此方法。

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

答案 1 :(得分:1)

是的,因为西蒙说要访问共享首选项,你需要上下文,所以为了使代码工作,我将添加这一行。

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

我没有正确地缩进它,我希望它有所帮助。

在给课堂打电话时,你应该这样打电话:

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

ConnectionClass connectionClass = new ConnectionClass(getActivity());//if in fragment

答案 2 :(得分:0)

你必须把它写入你的方法

{{1}}

或者你也可以将它写入constuctor