Java 这些静态getter&;setter方法-Android?

Java 这些静态getter&;setter方法-Android?,java,android,nullpointerexception,getter-setter,Java,Android,Nullpointerexception,Getter Setter,我被Android代码中的一个很小的问题弄糊涂了。我在自定义类(即“ResultsRoomInfoCustRLYT”)中创建的所有getter和setter都为我提供了以下“Java NullPointerException”: 05-31 13:37:13.222 29262-29262/com.whitsoft.stan E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at com

我被Android代码中的一个很小的问题弄糊涂了。我在自定义类(即“ResultsRoomInfoCustRLYT”)中创建的所有getter和setter都为我提供了以下“Java NullPointerException”:

05-31 13:37:13.222  29262-29262/com.whitsoft.stan E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.whitsoft.stan.mods.ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(ResultsRoomInfoCustRLYT.java:61)
        at com.whitsoft.stan.utils.DataFixer.updateRelevantViewsWithSelectedData(DataFixer.java:48)
        at com.whitsoft.stan.mods.StanListFragment.checkIfTheListAdapterDataHasChanged(StanListFragment.java:98)
        at com.whitsoft.stan.mods.StanListFragment.onActivityCreated(StanListFragment.java:49)
        at android.app.Fragment.performActivityCreated(Fragment.java:1707)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:921)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
        at android.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5493)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
        at dalvik.system.NativeStart.main(Native Method)
下面是自定义类('ResultsRoomInfoCustRLYT'),其中实现了getter和setter:

public class ResultsRoomInfoCustRLYT extends RelativeLayout {

LayoutInflater stanInflater;
private static TextView singleRoomsNumberTV, singleRoomsDescTV, vipRoomsNumberTV, vipRoomsDescTV;

public ResultsRoomInfoCustRLYT(Context context) {
    super(context);

    stanInflater = LayoutInflater.from(context);
    initializeAndLayoutChildren();
}

public ResultsRoomInfoCustRLYT(Context context, AttributeSet attrs) {
    super(context, attrs);

    stanInflater = LayoutInflater.from(context);
    initializeAndLayoutChildren();
}

public ResultsRoomInfoCustRLYT(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    stanInflater = LayoutInflater.from(context);
    initializeAndLayoutChildren();
}

private void initializeAndLayoutChildren() {

    stanInflater.inflate(R.layout.cust_rlyt_results_room_info, this, true);

    singleRoomsNumberTV = (TextView) findViewById(R.id.stan_Single_Rooms_Number_TV);
    singleRoomsDescTV = (TextView) findViewById(R.id.stan_Single_Rooms_Description_TV);
    vipRoomsNumberTV = (TextView) findViewById(R.id.stan_VIP_Rooms_Number_TV);
    vipRoomsDescTV = (TextView) findViewById(R.id.stan_VIP_Rooms_Description_TV);
}

public static String getSingleRoomsNumberTextValue() {
    return singleRoomsNumberTV.getText().toString();
}

public static void setSingleRoomsNumberTextValue(String singleRoomsNumberText) {
    singleRoomsNumberTV.setText(singleRoomsNumberText);
} 

public static void setVipRoomsNumberTextValue (String vipRoomsNumberText) {
    vipRoomsNumberTV.setText(vipRoomsNumberText);
}

public static String getVipRoomsDescTextValue () {
    return vipRoomsDescTV.getText().toString();
}

public static void setVipRoomsDescTextValue (String vipRoomsDescText) {
    vipRoomsDescTV.setText(vipRoomsDescText);
}  }
…正如您所看到的,我喜欢使用静态getter和setter,因为它让我可以通过一行代码轻松访问所需的视图。我在运行时遇到的关于这个错误的真正奇怪的事情是,我还有两个像这样的类,它们使用完全相同的设置-这些类工作正常(根据需要更新数据)。但是,当使用以下调用调用此类的setter时,它们都无法执行:

ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
    ResultsRoomInfoCustRLYT.setSingleRoomsDescTextValue(singleRoomsDescInfo);
    ResultsRoomInfoCustRLYT.setVipRoomsNumberTextValue(vipRoomsNumberInfo);
    ResultsRoomInfoCustRLYT.setVipRoomsDescTextValue(vipRoomsDescInfo);
任何帮助都将不胜感激。谢谢
Shore-T.

您的静态getter和setter访问由非静态方法初始化的静态引用-
initializeAndLayoutChildren
。在非静态方法中初始化静态变量时,在初始化静态变量之前访问静态变量会有风险。您还可能多次初始化静态变量——每次创建新实例时


例如,静态方法
setSingleRoomsNumberTextValue
访问静态变量
singleRoomsNumberTV
,该变量由
ResultsRoomInfoCustRLYT
的构造函数调用的实例方法
initializeAndLayoutChildren
初始化。如果在创建任何
ResultsRoomInfoCustRLYT
实例之前调用该静态方法,您将得到
NullPointerException

您的静态getter和setter访问由非静态方法初始化的静态引用-
initializeAndLayoutChildren
。在非静态方法中初始化静态变量时,在初始化静态变量之前访问静态变量会有风险。您还可能多次初始化静态变量——每次创建新实例时


例如,静态方法
setSingleRoomsNumberTextValue
访问静态变量
singleRoomsNumberTV
,该变量由
ResultsRoomInfoCustRLYT
的构造函数调用的实例方法
initializeAndLayoutChildren
初始化。如果在创建任何
ResultsRoomInfoCustRLYT
实例之前调用该静态方法,您将得到
NullPointerException

您的静态getter和setter访问由非静态方法初始化的静态引用-
initializeAndLayoutChildren
。在非静态方法中初始化静态变量时,在初始化静态变量之前访问静态变量会有风险。您还可能多次初始化静态变量——每次创建新实例时


例如,静态方法
setSingleRoomsNumberTextValue
访问静态变量
singleRoomsNumberTV
,该变量由
ResultsRoomInfoCustRLYT
的构造函数调用的实例方法
initializeAndLayoutChildren
初始化。如果在创建任何
ResultsRoomInfoCustRLYT
实例之前调用该静态方法,您将得到
NullPointerException

您的静态getter和setter访问由非静态方法初始化的静态引用-
initializeAndLayoutChildren
。在非静态方法中初始化静态变量时,在初始化静态变量之前访问静态变量会有风险。您还可能多次初始化静态变量——每次创建新实例时


例如,静态方法
setSingleRoomsNumberTextValue
访问静态变量
singleRoomsNumberTV
,该变量由
ResultsRoomInfoCustRLYT
的构造函数调用的实例方法
initializeAndLayoutChildren
初始化。如果在创建任何
ResultsRoomInfoCustRLYT
实例之前调用该静态方法,您将得到
NullPointerException

问题是您试图设置一些
null
文本视图的文本。例如,以下行:

ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
尝试执行以下操作:

singleRoomsNumberTV.setText(singleRoomsNumberText);
但什么是单人房MBERTV?在哪里分配?您需要拨打:

new ResultsRoomInfoCustRLYT(this);

从您的活动初始化静态文本视图。

问题是您试图将某些
文本视图的文本设置为空。例如,以下行:

ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
尝试执行以下操作:

singleRoomsNumberTV.setText(singleRoomsNumberText);
但什么是单人房MBERTV?在哪里分配?您需要拨打:

new ResultsRoomInfoCustRLYT(this);

从您的活动初始化静态文本视图。

问题是您试图将某些
文本视图的文本设置为空。例如,以下行:

ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
尝试执行以下操作:

singleRoomsNumberTV.setText(singleRoomsNumberText);
但什么是单人房MBERTV?在哪里分配?您需要拨打:

new ResultsRoomInfoCustRLYT(this);

从您的活动初始化静态文本视图。

问题是您试图将某些
文本视图的文本设置为空。例如,以下行:

ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
尝试执行以下操作:

singleRoomsNumberTV.setText(singleRoomsNumberText);
但什么是单人房MBERTV?在哪里分配?您需要拨打:

new ResultsRoomInfoCustRLYT(this);

从活动中初始化静态文本视图。

在调用setter之前是否运行了initializeAndLayoutChildren()函数?是否确定
findViewById
调用返回了正确的视图引用,而不是
null
(如果找不到传递的id,就会发生这种情况)。而且要快一点:静态getter和setter(例如变量)是非常糟糕的代码。哇,谢谢Tom-你把问题解决得很好。实际上,我有两个layout.xml文件用于这个自定义类(一个用于纵向视图,另一个用于