我有一个变量,我在主要活动中定义了它。就像这样:
public String TestVariable;
我在onCreate()
方法中设置变量的值,我有一个像这样的外层类:
public class Connection {
public void testMethod() {
/*I have to access TestVariable from this method */
}
}
我想我必须将活动传递给这个班级但是如何?
我正在Connection
类创建Application
类,如下所示:
private static Connection connection;
public Connection getConnectionInstance() {
if (connection == null)
connection = new Connection();
return connection;
}
所以我需要帮助。如何从TestVariable
Connection
答案 0 :(得分:0)
//A very simple way to do it
public class Connection {
public static String testVariable=null;
public void testMethod() {
/*I have to access TestVariable from this method */
}
}
// In the on create
void onCreate ()
{
this.testVariable="hello world";
Connection.testVariable=this.testVariable;
}
这应该有效,因为看到它testVariable值只设置一次,并在之后使用