这些静态 getter 和 setter 方法有什么问题 - Android?
What is wrong with these static getter & setter methods - Android?
我对 Android 代码中的一个非常小的问题感到困惑。我在自定义 class(即 '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.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)
这是实现 getter 和 setter 的自定义 class ('ResultsRoomInfoCustRLYT'):
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,因为它使我可以使用一行代码轻松访问所需的视图。关于我在运行时遇到的这个错误,真正奇怪的是我还有另外两个 classes 使用完全相同的设置 - 并且那些 classes 工作正常(根据需要更新数据).但是,当使用以下调用调用此 class 的设置器时,它们都无法执行:
ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
ResultsRoomInfoCustRLYT.setSingleRoomsDescTextValue(singleRoomsDescInfo);
ResultsRoomInfoCustRLYT.setVipRoomsNumberTextValue(vipRoomsNumberInfo);
ResultsRoomInfoCustRLYT.setVipRoomsDescTextValue(vipRoomsDescInfo);
任何帮助将不胜感激。谢谢。
肖尔-T.
您的静态 getter 和 setter 访问由非静态方法初始化的静态引用 - initializeAndLayoutChildren
。当您在非静态方法中初始化静态变量时,您 运行 有在初始化之前访问静态变量的风险。您还 运行 多次初始化静态变量的风险 - 每次创建新实例时。
例如,静态方法setSingleRoomsNumberTextValue
访问静态变量singleRoomsNumberTV
,该变量由ResultsRoomInfoCustRLYT
的构造函数调用的实例方法initializeAndLayoutChildren
初始化。如果在创建 ResultsRoomInfoCustRLYT
的任何实例之前调用该静态方法,您将得到 NullPointerException
.
嗯,问题是您正在尝试设置某些 null
文本视图的文本。例如,下面一行:
ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
尝试执行以下操作:
singleRoomsNumberTV.setText(singleRoomsNumberText);
但什么是 singleRoomsNumberTV?它分配在哪里?您需要致电:
new ResultsRoomInfoCustRLYT(this);
从您的 activity 初始化静态 TextView。
我对 Android 代码中的一个非常小的问题感到困惑。我在自定义 class(即 '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.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)
这是实现 getter 和 setter 的自定义 class ('ResultsRoomInfoCustRLYT'):
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,因为它使我可以使用一行代码轻松访问所需的视图。关于我在运行时遇到的这个错误,真正奇怪的是我还有另外两个 classes 使用完全相同的设置 - 并且那些 classes 工作正常(根据需要更新数据).但是,当使用以下调用调用此 class 的设置器时,它们都无法执行:
ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
ResultsRoomInfoCustRLYT.setSingleRoomsDescTextValue(singleRoomsDescInfo);
ResultsRoomInfoCustRLYT.setVipRoomsNumberTextValue(vipRoomsNumberInfo);
ResultsRoomInfoCustRLYT.setVipRoomsDescTextValue(vipRoomsDescInfo);
任何帮助将不胜感激。谢谢。 肖尔-T.
您的静态 getter 和 setter 访问由非静态方法初始化的静态引用 - initializeAndLayoutChildren
。当您在非静态方法中初始化静态变量时,您 运行 有在初始化之前访问静态变量的风险。您还 运行 多次初始化静态变量的风险 - 每次创建新实例时。
例如,静态方法setSingleRoomsNumberTextValue
访问静态变量singleRoomsNumberTV
,该变量由ResultsRoomInfoCustRLYT
的构造函数调用的实例方法initializeAndLayoutChildren
初始化。如果在创建 ResultsRoomInfoCustRLYT
的任何实例之前调用该静态方法,您将得到 NullPointerException
.
嗯,问题是您正在尝试设置某些 null
文本视图的文本。例如,下面一行:
ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
尝试执行以下操作:
singleRoomsNumberTV.setText(singleRoomsNumberText);
但什么是 singleRoomsNumberTV?它分配在哪里?您需要致电:
new ResultsRoomInfoCustRLYT(this);
从您的 activity 初始化静态 TextView。