[英]What is wrong with these static getter & setter methods - Android?
我對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)
這是實現getter和setter的自定義類('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,因為它使我可以用單行代碼輕松訪問所需的視圖。 我在運行時遇到的此錯誤的真正奇怪的是,我還有另外兩個這樣的類,它們使用完全相同的設置-並且這些類可以正常工作(根據需要更新數據)。 但是,當使用以下調用來調用此類的setter時,它們都將無法執行:
ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
ResultsRoomInfoCustRLYT.setSingleRoomsDescTextValue(singleRoomsDescInfo);
ResultsRoomInfoCustRLYT.setVipRoomsNumberTextValue(vipRoomsNumberInfo);
ResultsRoomInfoCustRLYT.setVipRoomsDescTextValue(vipRoomsDescInfo);
任何幫助都感激不盡。 謝謝。 邵氏T。
問題是您正在嘗試設置一些null
文本視圖的文本。 例如,以下行:
ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
嘗試執行以下操作:
singleRoomsNumberTV.setText(singleRoomsNumberText);
但是什么是singleRoomsNumberTV? 它分配在哪里? 您需要致電:
new ResultsRoomInfoCustRLYT(this);
從您的活動中初始化靜態TextView。
您的靜態getter和setter訪問通過非靜態方法initializeAndLayoutChildren
靜態引用。 在非靜態方法中初始化靜態變量時,存在在初始化之前訪問靜態變量的風險。 您還冒着多次初始化靜態變量的風險-每次創建新實例時。
例如,靜態方法setSingleRoomsNumberTextValue
訪問靜態變量singleRoomsNumberTV
其中得到由實例方法初始化initializeAndLayoutChildren
通過的構造稱為ResultsRoomInfoCustRLYT
。 如果在創建ResultsRoomInfoCustRLYT
任何實例之前調用該靜態方法,則會得到NullPointerException
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.