gpt4 book ai didi

java - 类数据设置后返回null

转载 作者:行者123 更新时间:2023-12-02 06:57:48 25 4
gpt4 key购买 nike

我有一个类,我通过标准 get/set 方法分配字符串值。设置值后,它会按预期返回它们。当我在另一个 fragment 中调用类值时,它们返回 null。我似乎无法弄清楚。

我如何设置类并设置值。

public class BasicInfo_Assets_Fragment extends Fragment {
View view
public static Store_Model store_model = null;
...

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

view = inflater.inflate(R.layout.fragment_basic_info, container, false);
...
zipCode = zip.getText().toString();
// set info
store_model = new Store_Model();
store_model.setZip(zipCode);
Log.v("STORE" , "Zip: " + store_model.getZip());
...

Log.v 打印出:“Zip: 47564”,与预期完全一致。但是,当我像这样在另一个 fragment 中调用该类时,它会给我一个错误。

public class Options_ListFragment extends ListFragment {
View view;
public static Store_Model store_model = BasicInfo_Assets_Fragment.store_model;
....
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_options, container, false);
Log.v("STORE", "Zip: " + store_model.getZip()); <!--- ERROR

如代码中所述,尝试提取该值会给我一个空指针异常。我不明白这个类(class)是如何失去其值(value)观的。也许我只是需要另一双眼睛。感谢您提前的帮助

编辑

感谢@Juan Jose Fidalgo 和@SJuan76。我想通了,并将 Options_ListFragment 中的代码更改为以下内容:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

view = inflater.inflate(R.layout.fragment_options, container, false);
if (BasicInfo_Assets_Fragment.store_model != null) {
LinearLayout ll = (LinearLayout) view
.findViewById(R.id.assets_store_info);
ll.setVisibility(View.VISIBLE);

store = (TextView) view.findViewById(R.id.store);
phone = (TextView) view.findViewById(R.id.phone);
address = (TextView) view.findViewById(R.id.address);
city = (TextView) view.findViewById(R.id.city);
zip = (TextView) view.findViewById(R.id.zip);
state = (TextView) view.findViewById(R.id.state);

getStoreInfo();
}

public void getStoreInfo() {
String mStore = BasicInfo_Assets_Fragment.store_model.getStoreNum();
String mPhone = BasicInfo_Assets_Fragment.store_model.getPhoneNum();
String mAddress = BasicInfo_Assets_Fragment.store_model.getAddress();
String mCity = BasicInfo_Assets_Fragment.store_model.getCity();
String mZip = BasicInfo_Assets_Fragment.store_model.getZip();
String mState = BasicInfo_Assets_Fragment.store_model.getState();

store.setText("Store #: " + mStore);
phone.setText("Phone #: " + mPhone);
address.setText("Address: " + mAddress);
city.setText("City: " + mCity);
zip.setText("Zip: " + mZip);
state.setText("State: " + mState);
}

最佳答案

您正在创建两个变量。一个作为 BasicInfo_Assets_Fragment 中的属性,另一个作为 Option_ListFragment 中的属性。它们是完全不同的变量。

如果您在 Option_ListFragment 中引用了 BasicInfo_Assets_Fragment 实例(我们称之为 biaf),则可以将其 View 属性引用为biaf.storeModel,这将给出你想要的。

最佳实践是将 storeModel 设为私有(private),并向 BasicInfo_Assets_Fragment 添加 getStoreModel() 方法。另外,请尝试遵循 Java 命名约定。

关于java - 类数据设置后返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17072976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com