Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SharedReferences-静态类中的静态字符串?_Java_Android - Fatal编程技术网

Java SharedReferences-静态类中的静态字符串?

Java SharedReferences-静态类中的静态字符串?,java,android,Java,Android,Java新手,很抱歉我理解力差。 我遇到了一个小问题,我有一个连接到服务器的静态类。我正在为SharedReferences使用另一个类,SharedReferences的一些详细信息是要连接到的服务器地址 由于我在静态连接类中使用了SharedReferences类,我遇到了一个缺陷,无法在静态上下文中使用非静态字段 这有什么关系吗?我可以将该值设置为静态吗? 或者,是否有方法加载这些值并使其保持静态 我会从任何活动获取我的共享参考资料: String sName = SharedPrefs.

Java新手,很抱歉我理解力差。 我遇到了一个小问题,我有一个连接到服务器的静态类。我正在为
SharedReferences
使用另一个类,
SharedReferences
的一些详细信息是要连接到的服务器地址

由于我在静态
连接
类中使用了
SharedReferences
类,我遇到了一个缺陷,无法在静态上下文中使用非静态字段

这有什么关系吗?我可以将该值设置为静态吗? 或者,是否有方法加载这些值并使其保持静态

我会从任何活动获取我的
共享参考资料

String sName = SharedPrefs.getserverName(this);
但无论如何,我在我的静态
连接
类中引用了这一点:

None-Static field XXXXXX cannot be referenced from a static context
(很明显,我不能在前面加上static这个词,因为它失败了,因为“这不能从静态上下文引用”。)

SharedReference
class:

private static SharedPreferences getPrefs(Context context) {
    return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}

public static String getserverName(Context context) {
    return getPrefs(context).getString("server_name", "");
}
我相信我正确地使用了静电计

所需的连接类:

public class ConnectionManager extends AppCompatActivity {
    static StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    static String serverName = SharedPrefs.getserverName(Context);
//  static String serverName ="192.168.1.105";   //TODO Ensure Database details loaded from Shared Preferences
    static String serverPort ="1433";
    static String databaseName ="DBANAME";
    static String db = String.format("jdbc:jtds:sqlserver://%s:%s/%s", serverName, serverPort, databaseName);
    static String un = "sa";
    static String pass = "";
    static Connection con;


    public static Connection getConnection() {

        try {
            try {
                StrictMode.setThreadPolicy(policy);
                Class.forName("net.sourceforge.jtds.jdbc.Driver");
                con = DriverManager.getConnection(db, un, pass);
            } catch (SQLException ex) {
                // log an exception. fro example:
                System.out.println("Failed to create the database connection.");
            }
        } catch (ClassNotFoundException ex) {
            // log an exception. for example:
            System.out.println("Driver not found.");
        }
        return con;
    }
}
查询活动(调用数据库连接类)

已更新

在用户1506104的帮助下,我实现了这一点。 我将对SharedRef的调用移动到getConnection内部并包含上下文。 像这样:

public static Connection getConnection(Context context) {
String serverName = SharedPrefs.getserverName(context);
String serverPort = SharedPrefs.getserverPort(context);
String databaseName = SharedPrefs.getdatabaseName(context);
String sPath = SharedPrefs.getserverPath(context);
String db = String.format("jdbc:jtds:sqlserver://%s:%s/%s", serverName, serverPort, databaseName);
try {
    try {
        StrictMode.setThreadPolicy(policy);
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        con = DriverManager.getConnection(db, un, pass);
    } catch (SQLException ex) {
        // log an exception. fro example:
        System.out.println("Failed to create the database connection.");
    }
} catch (ClassNotFoundException ex) {
    // log an exception. for example:
    System.out.println("Driver not found.");
}
return con;
   con = ConnectionManager.getConnection(this);
}

然后在我的活动中,我只需要将其包含在getConnection()中 像这样:

public static Connection getConnection(Context context) {
String serverName = SharedPrefs.getserverName(context);
String serverPort = SharedPrefs.getserverPort(context);
String databaseName = SharedPrefs.getdatabaseName(context);
String sPath = SharedPrefs.getserverPath(context);
String db = String.format("jdbc:jtds:sqlserver://%s:%s/%s", serverName, serverPort, databaseName);
try {
    try {
        StrictMode.setThreadPolicy(policy);
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        con = DriverManager.getConnection(db, un, pass);
    } catch (SQLException ex) {
        // log an exception. fro example:
        System.out.println("Failed to create the database connection.");
    }
} catch (ClassNotFoundException ex) {
    // log an exception. for example:
    System.out.println("Driver not found.");
}
return con;
   con = ConnectionManager.getConnection(this);

感谢您的时间:)

请使用单例设计模式。这是一个很好的编程实践。 创建静态类有上述问题。 这里是一个关于Android中单例模式的博客
如果您是从活动内部调用,则此代码没有问题,因为
This
关键字引用了您当前的活动实例

String sName = SharedPrefs.getserverName(this);
确保在静态连接类中有对上下文对象的引用。假设您的上下文对象被
context
变量引用,请执行以下操作:

String sName = SharedPrefs.getserverName(context);

这回答了你的问题吗?您的PREF_NAME是静态变量吗?您能在
连接
类中显示从编译器获得错误的代码吗?@Nhấ更新连接类后,行在以下位置失败:静态字符串serverName=SharedPrefs.getserverName(上下文);您必须将
上下文
传递给您的连接类。您可以这样做:
publicstaticconnectiongetconnection(Context-Context){…
然后当我调用它时,我需要:con=ConnectionManager.getConnection(Context-Context);或否?我想我可能陷入了困境,因为我在以前的广播公司接收器事件中使用了上下文。如果您愿意看一下,我可以向您展示完整的源代码?是的,您需要通过getConnection(上下文)提供上下文;因此,我再次更新了它,以显示调用Connection类以建立数据库连接的活动。当我从静态状态删除内容时,编译器会抱怨:错误:非静态方法getConnection()无法从静态上下文中引用,尽管我在查询活动中未看到任何引用到静态的内容。我昨天尝试了此操作,因为我不想反复重复相同的连接代码,我想从类中正确执行此操作,但该类不会以所需的格式加载我的首选项:(同样在股票查询活动中,执行以下操作:con=ConnectionManager.getConnection(this);