即使实例化后仍为空静态字段
Null static field even after instantiating
我有一个 class 静态字段和静态 setter 函数来设置它的值。
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()
由本机代码 cpp 调用。
在我的主要 activity 中,我将值初始化为
class myActivity extends Activity{
void onCreate(){
Intermediate.setIntermediate(new subType());
}
}
此处 subType
是 Type
class 的子class。
在本机方面,我正在调用 Intermediate class 的 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);
}
我有一个 class 静态字段和静态 setter 函数来设置它的值。
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()
由本机代码 cpp 调用。
在我的主要 activity 中,我将值初始化为
class myActivity extends Activity{
void onCreate(){
Intermediate.setIntermediate(new subType());
}
}
此处 subType
是 Type
class 的子class。
在本机方面,我正在调用 Intermediate class 的 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);
}