我有一个带有静态字段和静态设置函数来设置其值的类。
class Intermediate{
private static Type myObject=null;
public static void setIntermediate(Type ob){
myObject=ob;
}
public static String getValue(){
if(myObject!=null)
return myObject.getValue();
else
return ""; // <== always returning this value
}
}
Intermediate.getValue()
由 native 代码 cpp 调用。
在我的主要 Activity 中,我将值初始化为
class myActivity extends Activity{
void onCreate(){
Intermediate.setIntermediate(new subType());
}
}
这里subType
是Type
类的子类。
在 native 端,我调用 Intermediate 类的 getValue()
,其 myObject 始终为 null
;
最佳答案
您没有在代码中初始化子类型的字符串属性。您的 onCreate() 方法正在执行以下操作:
new subType()
因此,当调用 Intermediate.getValue() 时,您将遇到此行
return myObject.getValue();
您可以通过这样做来解决此问题
void onCreate(){
Type t = new subType();
t.setValue("whatever string you want");
Intermediate.setIntermediate(t);
}
关于java - 即使实例化后静态字段仍为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30397775/