2012-03-02 69 views
1

我想在我的项目中使用全局变量,所以我只需要一个可以轻松设置并获取任何变量数据的类。全局变量让我空

这样的:

public class Glob_variable { 


     String Path = new String(); 

     /**********************************************/ 
     public void setPath(String Path) { 
      this.Path = Path; 
     } 

     public String getPath() { 
      return Path; 
     } 
     /**********************************************/ 


    } 

但是,当我初始化它,我得到一个空例外。 我已经通过创建一个Glob_variable类的对象来初始化它。

像::

Glob_variable g = new Glob_variable(); 
     g.setPath("asdasd"); 

,当我在第二个活动叫我空变量时,我跟踪。 我已经叫这样的:

Glob_variable g = new Glob_variable(); 
g.getPath(); 

所以,你可以告诉我,在这一点上,我犯了一个错误?

回答

1

我想这是因为你的呼吁从另一个你Glob_variable类的实例获取方法。

Glob_variable g = new Glob_variable(); 
     g.setPath("asdasd"); 
     String path = g.getPath(); 

上面的代码是有效的。但是如果在类中使用上面的代码之后,在另一个类/活动中使用下面的代码,那么它是无效的,因为您在另一个类/活动中创建了另一个Glob_variable实例。

Glob_variable g = new Glob_variable(); 
String path = g.getPath(); 

尝试在你的代码以下单吨拍打,

public class Glob_variable 
{ 
    private Glob_variable GVariableClass; 
    private static String Path; 
    private Glob_variable() 
    { 
      GVariableClass = this; 
    } 

    public static Glob_variable getInstance() 
    { 
      if (GVariableClass == null) 
      { 
       GVariableClass = new Glob_variable(); 
      } 
      return GVariableClass; 
    } 

     /**********************************************/ 
     public void setPath(String Path) { 
      this.Path = Path; 
     } 

     public String getPath() { 
      return Path; 
     } 
     /**********************************************/ 

    } 
+0

可能是因为我在第二个activity中使用'Glob_variable g = new Glob_variable(); g.getPath();'所以可能是它一次创建新的实例....不是吗? – 2012-03-02 08:01:01

+0

是的,正好.... – Android 2012-03-02 08:06:12

+0

是的,你是对的....谢谢...... +1 – 2012-03-02 09:00:52

2

Glob_variable g = new Glob_variable();将创建新实例,使用static变量或应用singletone

Singletone:

Glob_variable.getInstance().setPath("abc"); 
String path = Glob_variable.getInstance().getPath("); 
+0

我以前使用过静态变量,但我没有成功一些时间 – 2012-03-02 06:34:37

+0

它总是相同,如果它是静态的,使用单身如果静态不是首选 – 2012-03-02 06:36:30

+0

静态变量应该工作。如果不成功,可能是由于其他错误。 – 2012-03-02 07:03:32

1

好,有很多答案在这里,但我觉得你像我一样也是来自VB(或者类似的东西)世界(我看到你的所有变量都是用第一个字母大写!)。

您必须明白,与VB不同,Java中没有“REAL”全局访问。当您使用'new'运算符时,会创建该类的新实例,并根据构造函数合约重新分配所有值。因此,请暂时关闭用于存储全局值的变量实例。

第二种选择是静态变量,但我看到你在使用它们时也有困难。在这里,我有一种强烈的感觉,有时你使用你的静态变量,然后给它们赋值。你必须确保你的引导者总是在你的获得者之前被调用。你甚至可以通过像使用Glob_variable.getPath(以下

public class Glob_variable { 


    static String Path = ""; 

    /**********************************************/ 
    public static void setPath(String Path) { 
     Glob_variable.Path = Path; 
    } 

    public static String getPath() throws Exception{ 
     if("".equals(Path)){ 
      throw new Exception("Variable no inited yet") 
     } 
     return Path; 
    } 
    /**********************************************/ 


} 

访问您的变量)自定义异常等尝试以下命名为Java约定为好,它会帮助你从长远来看:)

我发现下面的链接对理解singltons非常有用http://www.javacoffeebreak.com/articles/designpatterns/index.html请阅读。

是否全局值不变或必须在运行时设置?如果它们是恒定的,那么我会说你创建一个带有最终变量的接口并在代码中使用它们。