我对 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,因为它使我可以通过一行代码轻松访问所需的 View 。关于我在运行时遇到的这个错误,真正奇怪的事情是,我还有另外两个类似这个的类,它们使用完全相同的设置 - 并且这些类工作正常(根据需要更新数据)。但是,当使用以下调用调用此类的 setter 时,它们都无法执行:
ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
ResultsRoomInfoCustRLYT.setSingleRoomsDescTextValue(singleRoomsDescInfo);
ResultsRoomInfoCustRLYT.setVipRoomsNumberTextValue(vipRoomsNumberInfo);
ResultsRoomInfoCustRLYT.setVipRoomsDescTextValue(vipRoomsDescInfo);
任何帮助将不胜感激。谢谢。 海岸-T。
最佳答案
问题是您正在尝试设置某些 null
TextView 的文本。例如,以下行:
ResultsRoomInfoCustRLYT.setSingleRoomsNumberTextValue(singleRoomsNumberInfo);
尝试执行以下操作:
singleRoomsNumberTV.setText(singleRoomsNumberText);
但是 singleRoomsNumberTV 是什么?它被分配到哪里?您需要调用:
new ResultsRoomInfoCustRLYT(this);
从您的 Activity 中初始化静态 TextView。
关于java - 这些静态 getter 和 setter 方法有什么问题 - Android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30557778/